I Have to calculate date time difference how to do that in PHP. I need exact hours , mins and secs. any body have the scripts for that please give me :-P
The datediff
function at addedbytes.com lets you to just that easily :)
Check this.. This should work
<?php
function timeDiff($firstTime,$lastTime)
{
// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);
// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;
// return the difference
return $timeDiff;
}
//Usage :
echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32");
?>
For something like that use the built int time() function.
- Store the value of
time();
something like1274467343
which is the number of seconds scince - When ever needed retrive the value
and assign it to
$erlierTime
. - Assign
time()
again to the latter stop time you would want, lets just call it$latterTime
So now you have something like $erlierTime
and $latterTime
.
No just do $latterTime - $erlierTime
to get the difference in seconds and then do your divisions to get numb of minutes passed, num of hours passed etc.
In order for me or any one to give you a complete script you would need to tell us what your environment is like and what are you working with mysql, sqlite etc... also would need to know how that timer is triggered.
Not sure if I understand your question right, but if you are looking for some kind of stop watch, you could use following class:
class StopWatch {
private static $total;
public static function start() {
self::$total = microtime(true);
}
public static function elapsed() {
return microtime(true) - self::$total;
}
}
I use it for checking how long does it take to load a page. But it should give you general idea how to implement it basically anywhere. The difference is in seconds in this case, but changing that into proper time stamp shouldn't be difficult.