Jump to content

Calculate time difference in PHP


tmyonline

Recommended Posts

Hi guys, I need to calculate the time difference between t1 and t2, where

 

t1 = 4/2/2009 16:17, and

t2 = 4/27/2009 14:29

 

I did the following:

 

$t1 = strtotime('4/2/2009 16:17');

$t2 = strtotime('4/27/2009 14:29');

 

the delta_T = (t2 - t1) will give me a time stamp.  I would assume that this time stamp is expressed in seconds but it does not look correct in my calculation.  So, I'm wondering if the time difference can be calculated this way using the strtotime(), and if it is the way, is the unit of the time difference (or time stamp) is expressed in seconds ?  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/158980-calculate-time-difference-in-php/
Share on other sites

<?php
$t1 = strtotime('4/2/2009 16:17');
$t2 = strtotime('4/27/2009 14:29');

$delta_T = ($t2 - $t1);

$days = round(($delta_T % 604800) / 86400, 2); 
$hours = round((($delta_T % 604800) % 86400) / 3600, 2); 
$minutes = round(((($delta_T % 604800) % 86400) % 3600) / 60, 2); 
$seconds = round((((($delta_T % 604800) % 86400) % 3600) % 60), 2);
?>

 

One way.

Thanks Prismatic.  Would you please explain to me as to why

 

$hours = round((($delta_T % 604800) % 86400) / 3600, 2);  ?

 

In other words, I don't understand why I have to do a modulo (delta_T % 604800) and I have to do it again with 86400.  I understand that there are 86,400 seconds in a day and 604,800 seconds in a week but I don't know the meaning of the above operations.  Thanks.

 

 

Hi guys,

 

I don't understand the time calculation given by Prismatic.  I have waited for an answer for a week !!! Anyway, I would greatly appreciate any response.  Prismatic's formula does give me reasonable answers in terms of the numbers.  However, I wish to be able to interpret the meaning of that formula.  Any ideas ?  Thanks so much.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.