Connecting Tech Pros Worldwide Forums | Help | Site Map

Determine difference between two times

Newbie
 
Join Date: Aug 2007
Posts: 2
#1: Sep 24 '07
Hello again all,

I've come to a crossroads yet again, where spending hours trying to figure something out has led me to ask for help.

I am trying to take a start time and end time and find the difference between the two values.

I have a form that submits start time and end time. When you enter, say 1:30 as a start time, and 2:45 as a end time, the result should be 1:15.

I have tried date, explode, and strtotime all with no luck. I thought this was going to be a easy one, I was very wrong...

My code
Expand|Select|Wrap|Line Numbers
  1. $start = explode(":", $_GET['begin']);
  2. $start_hour = $start[0];
  3. $start_minute = $start[1];
  4.  
  5. $end = explode(":", $_GET['end']);
  6. $end_hour = $end[0];
  7. $end_minute = $end[1];
  8.  
  9. $result_minutes = $end_minute - $start_minute;
  10. $result_hours = $end_hour - $start_hour;
  11.  
  12. echo $result_hours . ":" . $result_minutes . "<Br>";
  13.  

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#2: Sep 24 '07

re: Determine difference between two times


Hi.

You could do something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $start = strtotime("12:00");
  3.     $end = strtotime("14:00");
  4.     $elapsed = $end - $start;
  5.     echo date("H:i", $elapsed);
  6. ?>
  7.  
But this is a flawed method. Consider what would happen if the start time was "23:00" and the end time was "1:00"... You would end up with "-22:00"

I would recommend using the time() function to record the start and end times. It would make this much more accurate.
brettl's Avatar
Member
 
Join Date: Sep 2007
Posts: 38
#3: Sep 24 '07

re: Determine difference between two times


Look at the mktime(). I used the code below to get the difference from today's date and another date. You'll have to break apart your start time and end time and use mktime and do a little math..... Hope this helps.

Expand|Select|Wrap|Line Numbers
  1. $today=time();
  2.     $pubdate="2007-09-24T07:02:14Z";
  3.     $year = substr($pubdate, 0, -16);
  4.     $month = substr($pubdate, 5, -13);
  5.     $day = substr($pubdate, 8, -10 );
  6.     $hour = substr($pubdate, 11, -7 );
  7.     $minute = substr($pubdate, 14 , -4);
  8.     $second = substr($pubdate, 17, -1);
  9.     $newpubdate = mktime($hour, $minute, $second, $month, $day, $year);
  10.     $difference = $today - $newpubdate;
  11.       echo 'Date: ';
  12.       echo floor($difference / 84600);
  13.       $difference -= 84600 * floor($difference / 84600);
  14.       echo ' days, ';
  15.       echo floor($difference / 3600);
  16.       $difference -= 3600 * floor($difference / 3600);
  17.       echo ' hours, ';
  18.       echo floor($difference / 60);
  19.       $difference -= 60 * floor($difference / 60);
  20.       echo ' minutes, and'. $difference.' seconds ago.';
  21.  
Reply