Reputation: 11091
My code is
$start_time= time();
echo "<BR><BR>";
echo "start: ".date("G:i:s",$start_time);echo "<BR>";
echo "now: ".date("G:i:s",time());echo "<BR>";
echo "difference: ".date("G:i:s",time()-$start_time);echo "<BR>";
echo "<BR><BR>";
echo time()-$start_time;
echo "<BR><BR>";
and the output is
start: 12:03:41
now: 12:06:04
difference: 10:02:23
143
Any idea why number of hours is not 0?
Upvotes: 0
Views: 281
Reputation: 23848
Have you tried explicitly setting a time zone for you script?
date_default_timezone_set('UTC');
Upvotes: 1
Reputation: 2256
Difference should just be time() - $start_time to give the number of seconds it takes to run the script. If you pass it to date, then it will give the date 143 seconds after the Unix Epoch. Make the date format 'Y/m/d G:i:s' to see exactly what's going on there.
Upvotes: 1
Reputation: 11098
I think you should use unix timestamp when calculating date differences.
See below
$start_time= date("U", time());
sleep(5); //stops execution for 5 seconds
$endtime = date("U",time());
echo date("G:i:s",$endtime - $start_time); //Outputs 0:00:05
or something like this for actual date / time differences
$start_time= date("U", strtotime("11/11/11 12:53"));
$endtime = date("U",strtotime("11/11/11 12:59"));
echo date("G:i:s",$endtime - $start_time);
Outputs 0:06:00
6 minutes
Upvotes: 1