newphpbees Posted January 30, 2012 Share Posted January 30, 2012 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.. Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/ Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2012 Share Posted January 30, 2012 Without a date, there is no way to tell what the difference between 2 times actually is; it's ambiguous. Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/#findComment-1312441 Share on other sites More sharing options...
newphpbees Posted January 30, 2012 Author Share Posted January 30, 2012 if there is a date...how??? Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/#findComment-1312442 Share on other sites More sharing options...
AyKay47 Posted January 30, 2012 Share Posted January 30, 2012 How do we know if these are times from today, yesterday, or 20 years ago? This is why a date is needed. Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/#findComment-1312443 Share on other sites More sharing options...
newphpbees Posted January 30, 2012 Author Share Posted January 30, 2012 Do you have any example code? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/#findComment-1312444 Share on other sites More sharing options...
Pikachu2000 Posted January 30, 2012 Share Posted January 30, 2012 Where are the values coming from? Are they stored in a database maybe? Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/#findComment-1312447 Share on other sites More sharing options...
newphpbees Posted January 30, 2012 Author Share Posted January 30, 2012 No... It was inputted in a textbox... Thank you Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/#findComment-1312454 Share on other sites More sharing options...
ignace Posted January 30, 2012 Share Posted January 30, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/#findComment-1312520 Share on other sites More sharing options...
PaulRyan Posted January 30, 2012 Share Posted January 30, 2012 Edit - Overlooked something, apologies. Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/#findComment-1312562 Share on other sites More sharing options...
PaulRyan Posted January 30, 2012 Share Posted January 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/#findComment-1312573 Share on other sites More sharing options...
newphpbees Posted February 6, 2012 Author Share Posted February 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/#findComment-1314998 Share on other sites More sharing options...
ignace Posted February 6, 2012 Share Posted February 6, 2012 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); } Quote Link to comment https://forums.phpfreaks.com/topic/256018-issue-in-getting-the-time-difference-between-two-variables/#findComment-1315021 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.