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
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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.