Re: Difference between 2 time entries

From: Date: Thu, 20 Jul 2006 15:54:08 +0000
Subject: Re: Difference between 2 time entries
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-239639@lists.php.net to get a copy of this message
At 12:41 PM +0200 7/20/06, Chris Grigor wrote: >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?? > >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"); Chris: You're about there, you have the seconds, just add this: $hours = floor($timeDiff/3600); $timeDiff = $timeDiff - ($hours * 3600); $minutes = floor($timeDiff/60); $timeDiff = $timeDiff - ($minutes * 60); $seconds = $timeDiff; echo($hours); echo("<br/>"); echo($minutes); echo("<br/>"); echo($seconds); I'm surprised that php doesn't have a function to do this -- but maybe I didn't RTFM enough. In any event, hth's tedd -- ------------------------------------------------------------------------------------ http://sperling.com http://ancientstones.com http://earthstones.com

« previous php.general (#239639) next »