Forum Moderators: coopster

Message Too Old, No Replies

date time maths

addition and subtraction of dates and times in specific format

         

timemachine

1:17 am on Dec 16, 2009 (gmt 0)

10+ Year Member



Hi,

I am required to do some calcualtions of dates and times using a specific format as input.

eg I have case raised at 14-DEC-2009 11:34:14 and the case was accepted by an operator on 15-DEC-2009 14:23:44, I would like to be able to calculate the total time it took to be accepted in the following fomat dd:hh:mm:ss.

Now I have tried to convert 14-DEC-2009 11:34:14 using strtotime() but I think it is the format of the date and time that is throwing it off.

The date and time stamp input is always in this format i.e. 14-DEC-2009 11:34:14.

Is there a function that can do this or is it a case of trying to massage the format into another format then perform the calculations and then convert it back.

much appreciate any help guys.

mooger35

5:01 pm on Dec 16, 2009 (gmt 0)

10+ Year Member



strtotime should work for you.

$date= '14-DEC-2009 11:34:14';

$timestamp = strtotime($date); // returns 1260819254

CyBerAliEn

6:24 pm on Dec 16, 2009 (gmt 0)

10+ Year Member



^ True. Entering your date format into PHP's strtotime() returns the correct UNIX timestamp.

To do what you want to do, you should get both times into timestamps using strtotime(), ie:

$timeStart = strtotime($date1);
$timeEnd = strtotime($date2);
$diff = ($timeEnd-$timeStart);

The variable 'diff' will tell you how many seconds have elapsed between the two dates. With this, you can then derive out the time in the format such as: DD:HH:MM:SS (etc)

My recommendation would be to create a function such as the following...

function myTime($diff)
{
$days = floor($diff/(24*60*60));
$hours = floor(($diff-($days*24*60*60))/(60*60));
//etc
return "{$days}:{$hours}:{$mins}:{$secs}";
}

The above is very rough. But you should get an idea. Using a function like the above, you can take any time difference (in seconds) and get it in your desired output. You could also create a wrapper function that has inputs date1 and date2, finds the difference, plugs it into a function like the above... and you get out the time difference in your desired output. There's a lot of options to do what you want. Unfortunately, I am not aware of any built-in PHP functions that do exactly what you want to do... though using "strtotime()" helps take of a lot of the more complicated work for you.

timemachine

4:02 am on Dec 17, 2009 (gmt 0)

10+ Year Member



thank you all for assistance, it did the trick, I am still learning php so thanks for the quick lesson as well

timemachine

6:48 am on Dec 17, 2009 (gmt 0)

10+ Year Member



The final script calcs used were:

$days = floor($diff/(24*60*60));
$hours = floor(($diff-($days*24*60*60))/60*60);
$mins = floor((($diff-($days*24*60*60)- ($hours*60*60))/60));
$secs = floor(($diff-($days*24*60*60) - ($hours*60*60))-($mins*60));

print "{$days}days:{$hours}hours:{$mins}mins:{$secs}sec";

again thank you all for the help.