Jump to content

Issue in getting the time difference between two variables


newphpbees

Recommended Posts

I have two textboxes that inputted time, and I want to get the difference between two time.

 

for example:

 

$IN = 13:35;

$OUT = 17:35;

 

$OTHours = ($OUT - $IN);

 

$OTHours = 4.00;

 

 

and it is correct, but I have a scenario like this:

 

$IN = 21:35;

$OUT = 05:35;

 

$OTHours = -16.00;

 

it should be 8.00.

 

Any help is highly appreciated.

 

Thank you..

I wrote this. Assuming both times are on the same day.

 

try {
  $t1 = new Time($argv[1]);
  $t2 = new Time($argv[2]);
  echo $t1->diff($t2);
} catch (Exception $e) {
  echo $e->getMessage();
}

class Time {
private $seconds;
public function __construct($time) {
  if (!self::valid($time))
    throw new InvalidArgumentException('Invalid time. Type: ' . gettype($time));
  $this->seconds = $this->_toSeconds($time);
}
public static function valid($time) {
  if (!is_string($time))
    return false;
  return preg_match('~^(\d|0\d|1\d|2[0-3])\d|[0-5]\d)$~', $time);
}
public function diff(Time $t) {
  $t1   = $this->toSeconds();
  $t2   = $t->toSeconds();
  $diff = ($t1 - $t2);

  if ($diff === 0)
    return '00:00';
  return $diff < 0 ? $this->_toString($t2 - $t1) : $this->_toString($diff);
}
private function _toSeconds($time) {
  if (!self::valid($time))
     throw new InvalidArgumentException('Invalid time. Type: ' . gettype($time));
  sscanf($time, '%d:%d', $h1, $m1);
  return ($h1 * 3600) + ($m1 * 60);
}
public function toSeconds() {
  return $this->seconds;
}
private function _toString($seconds) {
  if (!is_int($seconds)) return false;
  return floor($seconds / 3600) . ':' . floor(($seconds % 3600) / 60);
}
}

 

>php time.php 13:35 17:35

4:00

>php time.php 21:35 05:35

16:00 # reverses the arguments

Fixed my issue from my above post.

 

Something a little bit simpler:

<?PHP
     
  function time_difference($IN,$OUT) {
    $IN  = preg_replace('#[:]#','.',$IN);
    $OUT = preg_replace('#[:]#','.',$OUT);
    
    if($IN-$OUT < 0) {
      $OTHours = number_format($OUT - $IN,2);
    } else {
      $OTHours = explode('.',number_format(((24-$IN-0.4)+$OUT),2));
      
      if($OTHours[1] > 59) {
        $OTHours[1] -= 60;
        $OTHours[0] += 1;
      }
      
      $OTHours = number_format(implode('.',$OTHours),2);
    }
    return $OTHours;
  }

  $IN = '21:35';
  $OUT = '05:35';
  
  echo time_difference($IN,$OUT);
  
?>

 

Regards, PaulRyan.

I have this new code:

$from_time  = strtotime($IN);
$to_time = strtotime($OUT);
$OTHours = round(abs($to_time - $from_time) / 3600);

 

$IN = 2011-12-22 13:30;

$OUT = 2011-12-22 15:30;

 

from this code the $OTHours result is 1.00

it should be 1.30

 

by the way the OTHours field is Decimal Type.

Here's the calculation you are looking for.

 

private function _toString($seconds) {
  if (!is_int($seconds)) return false;
  return floor($seconds / 3600) . ':' . floor(($seconds % 3600) / 60);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.