johnadamson Posted May 16, 2007 Share Posted May 16, 2007 HELP!! I am trying to calculate the difference between 2 time strings. e.g: $time1 = "14:00:03"; $time2 = "12:30:03"; The difference needs to be in hours:minutes:seconds. The 2 times will always be different as they are taken from a database (the 2 times above are just examples of how the time is presented). I would VERY much appreciate any help. Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/ Share on other sites More sharing options...
nikkieijpen Posted May 16, 2007 Share Posted May 16, 2007 <?php $time1 = "14:00:03"; $time2 = "12:30:03"; echo (strtotime($time1) - strtotime($time2)); // displays the time difference in seconds. ?> Quote Link to comment https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/#findComment-254366 Share on other sites More sharing options...
johnadamson Posted May 16, 2007 Author Share Posted May 16, 2007 is there no way I can display it in hours:minutes:seconds? Or if I used the code you sent me nikkieijpen, can I work out hours:minutes:seconds from the seconds? Quote Link to comment https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/#findComment-254381 Share on other sites More sharing options...
jitesh Posted May 16, 2007 Share Posted May 16, 2007 <?php $time1 = "14:00:03"; $time2 = "12:30:03"; echo date("H:i:s",(strtotime($time1) - strtotime($time2))); // displays the time difference in seconds. ?> Quote Link to comment https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/#findComment-254387 Share on other sites More sharing options...
johnadamson Posted May 16, 2007 Author Share Posted May 16, 2007 Fantastic jitesh!! That worked an absolute treat. Thanks for your help. Thanks to nikkieijpen also. Quote Link to comment https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/#findComment-254403 Share on other sites More sharing options...
johnadamson Posted May 21, 2007 Author Share Posted May 21, 2007 The code has been working FANTASTIC, but for some reason I am seeing the following: $time1 = "12:55:00"; $time2 = "12:55:32"; echo date("H:i:s",(strtotime($time2) - strtotime($time1))); When I run this it comes back saying that the time difference is 01:00:32, when it should be 00:00:32 (32 seconds). Any ideas would be very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/#findComment-258122 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 Code is fine.. i just tested it as i couldn't see a problem try saving and re-uploading EDIT: i got 00:00:32 Quote Link to comment https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/#findComment-258138 Share on other sites More sharing options...
johnadamson Posted May 21, 2007 Author Share Posted May 21, 2007 I have just tried it again and get the same. I tried using different value: $time1 = "06:55:00"; $time2 = "08:55:32"; echo date("H:i:s",(strtotime($time2) - strtotime($time1))); and I get 03:00:32 when it should be 02:00:32. I really have got no idea why it is doing it. Quote Link to comment https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/#findComment-258142 Share on other sites More sharing options...
MadTechie Posted May 21, 2007 Share Posted May 21, 2007 tested that and got 02:00:32 create a new file that contains only that code ie test.php <?php $time1 = "06:55:00"; $time2 = "08:55:32"; echo date("H:i:s",(strtotime($time2) - strtotime($time1))); ?> Quote Link to comment https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/#findComment-258147 Share on other sites More sharing options...
jitesh Posted May 21, 2007 Share Posted May 21, 2007 If you are connecting to database then better to make diff by query like this SELECT TIMEDIFF('12:55:32', '12:55:00') or <?php /** * Function to calculate date or time difference. * * Function to calculate date or time difference. Returns an array or * false on error. * * @author J de Silva <giddomains@gmail.com> * @copyright Copyright © 2005, J de Silva * @link http://www.gidnetwork.com/b-16.html Get the date / time difference with PHP * @param string $start * @param string $end * @return array */ function get_time_difference( $start, $end ) { $uts['start'] = strtotime( $start ); $uts['end'] = strtotime( $end ); if( $uts['start']!==-1 && $uts['end']!==-1 ) { if( $uts['end'] >= $uts['start'] ) { $diff = $uts['end'] - $uts['start']; if( $days=intval((floor($diff/86400))) ) $diff = $diff % 86400; if( $hours=intval((floor($diff/3600))) ) $diff = $diff % 3600; if( $minutes=intval((floor($diff/60))) ) $diff = $diff % 60; $diff = intval( $diff ); return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) ); } else { trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING ); } } else { trigger_error( "Invalid date/time data detected", E_USER_WARNING ); } return( false ); } // a START time value $start = '12:55:00'; // an END time value $end = '12:55:32'; // what is the time difference between $end and $start? if( $diff=@get_time_difference($start, $end) ) { echo "Hours: " . sprintf( '%02d:%02d:%02d', $diff['hours'], $diff['minutes'],$diff['seconds'] ); } else { echo "Hours: Error"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/#findComment-258148 Share on other sites More sharing options...
johnadamson Posted May 21, 2007 Author Share Posted May 21, 2007 Tried creating a new file MadTechie and it still did the same. VERY STRANGE!! The code you provided me jitesh worked well. Thank you. Just a shame I could not get the original php code to work. Thanks for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/51634-solved-difference-between-2-time-strings/#findComment-258171 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.