how to find the difference between two times in PHP?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • impin
    New Member
    • Jul 2010
    • 127

    how to find the difference between two times in PHP?

    i have stored the user break times in the database.

    break out time and break in time... now i have to find the break time.

    that is ( break in time - break out time).

    break out time = 2010-10-22 10:27:10
    break in time = 2010-10-22 10:50:10
    these values are stored in the db.
    so the break time is 23 mins...

    how i find this? plz help...
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    In PHP convert it to timestamp using strtotime, subtract and the answer will be in seconds.
    Then divide by the units required, i.e. 1 day = 86400 secs

    In SQL use DATEDIFF or DATEADD with negative values

    Comment

    • impin
      New Member
      • Jul 2010
      • 127

      #3
      thanks. i find the ansewer...

      using this function.

      Code:
       function dateDiff($time1, $time2, $precision = 6) {
        
          if (!is_int($time1)) {
            $time1 = strtotime($time1);
          }
          if (!is_int($time2)) {
            $time2 = strtotime($time2);
          }
       
         
          if ($time1 > $time2) {
            $ttime = $time1;
            $time1 = $time2;
            $time2 = $ttime;
          }
       
        
          $intervals = array('year','month','day','hour','minute','second');
          $diffs = array();
       
          
          foreach ($intervals as $interval) {
      
            $diffs[$interval] = 0;
      
            $ttime = strtotime("+1 " . $interval, $time1);
        
            while ($time2 >= $ttime) {
      	$time1 = $ttime;
      	$diffs[$interval]++;
      
      	$ttime = strtotime("+1 " . $interval, $time1);
            }
          }
       
          $count = 0;
          $times = array();
        
          foreach ($diffs as $interval => $value) {
      
            if ($count >= $precision) {
      	break;
            }
            
            if ($value > 0) {
      
      	if ($value != 1) {
      	  $interval .= "s";
      	}
      
      	$times[] = $value . " " . $interval;
      	$count++;
            }
          }
       
       
          return implode(", ", $times);
        }
      Code:
      $break= dateDiff("$breakin", "$breakout") . "\n";

      now i want to add the times intervals...

      they user take several breaks in single day. i want to add all the break duration. so i can get the total break time for the user in the whole day...

      how to do it?

      users first break time duration 30 mins.
      second break duration 25 mins
      third break duration 20 mins

      i am getting like this...

      now i want to add 3 break duartions. so the users total break time is 1 hour and 15 mins...

      how to do it?

      Comment

      • charles07
        New Member
        • Dec 2011
        • 45

        #4
        impin you can easily do it in mysql

        SELECT TIMEDIFF('2010-10-22 10:50:10','2010-10-22 10:27:10') AS timedifference
        this would give the difference as 00:23:00

        i believe break in time & break out time are stored in DB

        Comment

        Working...