Re: Difference between 2 time entries
| From: | nicolas figaro | Date: | Thu, 20 Jul 2006 11:51:02 +0000 |
| Subject: | Re: Difference between 2 time entries | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-239617@lists.php.net to get a copy of this message | ||
Chris Grigor a écrit :
Morning all, I am looking to get the differnce in hours / minutes between 2 values. Currently I have 2 time entries being retruned from mysql, one which is a start time and the other which is a finish time. So $start = '13:12:17'; $finish = '23:12:17'; How would one get the differnce between these 2 times?? IMHO, the best is to generate a timestamp using mktime for each date.you can the calculate the difference of timestamps and convert it back using date. $tm_start = mktime(substr($start,0,2),substr($start,3,2), substr($start,5,2)); $tm_finish = mktime(substr($finish,0,2),substr($finish,3,2), substr($finish,5,2)); $tm_diff = $tm_finish -tm_start; print date("H:i",$tm_diff); I don't know if date accepts negative timestamps, so be sure $finish is later than $start (you can also put the day in case $finish = "00:00:12" and $start ="15:00:00"). hope this'll help N F
I have looked at using the following but am not to sure.... 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("$start","$finish"); Thanks ChrisNicolas Figaro <http://www.sdv.fr> <nfigaro@sdv.fr <mailto:nfigaro@sdv.fr>> SDV plurimédia