ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Subtracting one time from time

December 27, 2011 5:08pm

Subscribe [4]
  • #1 / Dec 27, 2011 5:08pm

    Jose B

    313 posts

    I have created StartTime and EndTime fields and wish to subtract the StartTime from the Endtime to get how many hours and minutes someone has worked. I am trying to avoid having a datetime field as that would mean having to enter mm/dd/yy hh:mm:ss for each and every entry both start and end times. Too much typing considering I have 45 students who time I have to track each day for a year.  I want to avoid having to input the date as I have a separate (just one) date fields. Where can I obtain further info on working with dates? I found just one wiki on it and it had to do more with formatting time than calculating time. Any help would be appreciated.

  • #2 / Dec 28, 2011 4:16am

    ahmad saad

    364 posts

    try this php code

    $startdate="{entry_date}"; $enddate="{expiration_date}";
    
       $subtract = floor($enddate-$startdate);
       $remainhour=floor($subtract/3600);
       $remainminutes=floor(($subtract-($remainhour*3600) )/60) ;
       if ($remainhour > 0)echo($remainhour." hours ".$remainminutes." minutes");
  • #3 / Dec 28, 2011 7:16am

    John Henry Donovan

    12339 posts

    Thanks ahmad. Did that work for you Jose?

    Going to move this thread to Community Help forum in the mean time as more appropriate there.

  • #4 / Dec 28, 2011 3:30pm

    Jose B

    313 posts

    I am sure that the above PHP code will work. And ultimately, I may have to use it. But the PHP code above uses EE’s Entry_Date and Expiration_Date both of which mean having to input the date. That is what I am trying to avoid, and is more work than I need to do.


    I am trying to avoid using any DateTime type of field and just want to input the time. For some reason EE, MySql and MS Access all have a DateTime field with which one can subtract one time from another, but not at simple Time field with which to do the same. I do not want to have to type the Year, Month and Date every time I enter the time.

    With the application I have in mind, I have to enter the Start Time and End Time each day, 5 days a week for 45 students for a year. If I could just type the time without the date part I save 22 keystrokes per record and a lot of time. Adding 09:30 or 15:30 versus 04/10/11 09:30 AM is a big difference (9,000 keystrokes every two weeks). And the extra keystrokes can introduce errors. Sometimes I can not do the typing daily and have to do a whole week’s worth at one sitting.

    It is frustrating that in order to work with time I must add the Date when it has nothing to do with what I am doing. Even if I was not going to do calculations with the time, even if I was just going to input the time, I have no desire to input a date. I could add one separate date field if needed and not have to input the date twice (Start Time and End Time).

    Surely, I am not the only person who wants to subtract one time from another based on a 24 hour day without wanting to enter the Date? Am I? Your thoughts on this matter are appreciate.  I will use the above code on one of my websites which already uses Entry_Date. A million thanks for the code.

    Jose

  • #5 / Dec 29, 2011 2:59am

    ahmad saad

    364 posts

    please see http://devot-ee.com/add-ons/time-select

    I hope this is helpfull.

  • #6 / Dec 29, 2011 5:18pm

    Jose B

    313 posts

    Thanks for the info Ahmad, but this is not what I am looking for. Ultimately I want to have two fields, StartTime, EndTime. Then I wish calculate how long someone was at work (hh:mm) based on the difference between the StartTime and EndTime (on the same day, no going into the previous or next day). I am surprised by the lack of information EE has about time.

  • #7 / Dec 30, 2011 11:41am

    That time-select add-on looks like about the closest you’ll get using an existing add-on.

    If you need a pure EE solution, the next best hack might be storing it as a pair of text fields and then doing some PHP in a template to parse the data and perform the math.

    I’m not certain what your requirements are, but, something like a spreadsheet on Google Docs might be a more elegant way to accomplish this.

  • #8 / Dec 30, 2011 1:13pm

    Adrienne L. Travis

    213 posts

    There is actually a TIME datatype in MySQL, but there isn’t any fieldtype in EE that takes advantage of it. Maybe I’ll whip something up, but it’s a little bit complicated because by default all field columns are TEXT.

  • #9 / Dec 30, 2011 10:54pm

    Jose B

    313 posts

    Guys, the reason I have a StartTime and EndTime fields is because I will then need to produce a calculated field which will subtract the StartTime from the EndTime and come up with total HH:MM for that day. The time select is not necessary as my students come in (late) at different times and we input the exact time they come in. As our students also get paid for attending school (GED), time in school is important and determines their stipend. I will probably fore go using EE and create my own MySql/PHP application. It seems I will have to use the DateTime field as it is the only one with which the DateDiff function works. Thanks for all your import. I guess EE is not a good fit for every application.

    Still, I encourage EE creators to create a more robust Time field which one can add or subtract time from.

  • #10 / Aug 21, 2012 3:06pm

    OrganizedFellow

    435 posts

    Has there been any solution to this Jose B?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases