Online Shopping : Computers : Programming : Languages : PHP : Function Libraries : Date and Time

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

39 of 68 people (57%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How can you calculate the difference between 2 times?

May 4th, 2000 16:54
Jay, Christian Schmidt, Greg Wake, Nathan Wallace,


Assuming that you have two times in the format returned by time() and
mktime() (or e.g. UNIX_TIMESTAMP() in MySql) you simply simply subtract
the two. The result is the difference measured in seconds.

Then divide the difference by 86400 which is the seconds in a day

Here's an example 4 ya:

$bdnum="04/05/2000";
$ednum="05/05/2000";

$from_month = substr($bdnum, 0,2);
$from_day =  substr($bdnum, 3,2);
$from_year =  substr($bdnum, 6,4);
$till_month =  substr($ednum, 0,2);;
$till_day = substr($ednum, 3,2);
$till_year = substr($ednum, 6,4);

$from_date = mktime(0, 0, 0, $from_month, $from_day, $from_year);
$till_date = mktime(0, 0, 0, $till_month, $till_day, $till_year);

for($ts = $from_date; $ts <= $till_date; $ts+=86400) {
	if ($begtimestamp=="") { 
	    $begtimestamp=$ts; //this line freezes first timestamp
        }
// echo $ts;    //echos the timestamps
// echo date("l, F d Y.",$ts);	//echos the actual Day, month day year
	}
$endtimestamp=$ts; //this line freezes the last timestamp

$totalsecs = ($endtimestamp-$begtimestamp) / 86400;