3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * Workaround for PHP < 5.3.0 */ if(!function_exists('date_diff2')) { class DateInterval1 { public $y; public $m; public $d; public $h; public $i; public $s; public $invert; public $days; public function format($format) { $format = str_replace('%R%y', ($this->invert ? '-' : '+') . $this->y, $format); $format = str_replace('%R%m', ($this->invert ? '-' : '+') . $this->m, $format); $format = str_replace('%R%d', ($this->invert ? '-' : '+') . $this->d, $format); $format = str_replace('%R%h', ($this->invert ? '-' : '+') . $this->h, $format); $format = str_replace('%R%i', ($this->invert ? '-' : '+') . $this->i, $format); $format = str_replace('%R%s', ($this->invert ? '-' : '+') . $this->s, $format); $format = str_replace('%y', $this->y, $format); $format = str_replace('%m', $this->m, $format); $format = str_replace('%d', $this->d, $format); $format = str_replace('%h', $this->h, $format); $format = str_replace('%i', $this->i, $format); $format = str_replace('%s', $this->s, $format); return $format; } } function date_diff2(DateTime $date1, DateTime $date2) { $diff = new DateInterval1(); if($date1 > $date2) { $tmp = $date1; $date1 = $date2; $date2 = $tmp; $diff->invert = 1; } else { $diff->invert = 0; } $diff->y = ((int) $date2->format('Y')) - ((int) $date1->format('Y')); $diff->m = ((int) $date2->format('n')) - ((int) $date1->format('n')); if($diff->m < 0) { $diff->y -= 1; $diff->m = $diff->m + 12; } $diff->d = ((int) $date2->format('j')) - ((int) $date1->format('j')); if($diff->d < 0) { $diff->m -= 1; $diff->d = $diff->d + ((int) $date1->format('t')); } $diff->h = ((int) $date2->format('G')) - ((int) $date1->format('G')); if($diff->h < 0) { $diff->d -= 1; $diff->h = $diff->h + 24; } $diff->i = ((int) $date2->format('i')) - ((int) $date1->format('i')); if($diff->i < 0) { $diff->h -= 1; $diff->i = $diff->i + 60; } $diff->s = ((int) $date2->format('s')) - ((int) $date1->format('s')); if($diff->s < 0) { $diff->i -= 1; $diff->s = $diff->s + 60; } $start_ts = $date1->format('U'); $end_ts = $date2->format('U'); $days = $end_ts - $start_ts; $diff->days = round($days / 86400); if (($diff->h > 0 || $diff->i > 0 || $diff->s > 0)) $diff->days += ((bool) $diff->invert) ? 1 : -1; return $diff; } function formatHour($hours, $minutes) { // format info if ($minutes < 0) { // minutes < 0 // sub hours $hours -= floor(-$minutes / 60) + 1; // make minutes to objective $minutes += (floor(-$minutes / 60) + 1) * 60; } else { // minute > 0 // add hours $hours += floor($minutes / 60); // make minutes to < 60 $minutes -= floor($minutes / 60) * 60; } // make sure minutes patter in mm if ($minutes < 10) { $minutes = "0" . $minutes; } // return pattern hours:minutes return $hours . ':' . $minutes; } } // array(['2014-03-17 16:07:52', '2014-03-17 17:43:09'], '1:35'), $start_time = "2014-03-17 16:07:52"; $end_time = "2014-03-17 17:43:09"; $start_time = new DateTime($start_time); $end_time = new DateTime($end_time); $interval = date_diff2($start_time, $end_time); list($days, $hours, $minutes) = explode(':', $interval->format('%d:%h:%i')); $hours += ($days * 24); print_r(formatHour($hours, $minutes)); echo '<br />'; echo phpversion();
Output for 7.1.0
1:35<br />7.1.0
Output for 7.0.14
1:35<br />7.0.14
Output for 7.0.10
1:35<br />7.0.10
Output for 7.0.9
1:35<br />7.0.9
Output for 7.0.8
1:35<br />7.0.8
Output for 7.0.7
1:35<br />7.0.7
Output for 7.0.6
1:35<br />7.0.6
Output for 7.0.5
1:35<br />7.0.5
Output for 7.0.4
1:35<br />7.0.4
Output for 7.0.3
1:35<br />7.0.3
Output for 7.0.2
1:35<br />7.0.2
Output for 7.0.1
1:35<br />7.0.1
Output for 7.0.0
1:35<br />7.0.0
Output for hhvm-3.12.0
1:35<br />7.0.99-hhvm
Output for hhvm-3.10.0
1:35<br />5.6.99-hhvm
Output for 5.6.28
1:35<br />5.6.28
Output for 5.6.25
1:35<br />5.6.25
Output for 5.6.24
1:35<br />5.6.24
Output for 5.6.23
1:35<br />5.6.23
Output for 5.6.22
1:35<br />5.6.22
Output for 5.6.21
1:35<br />5.6.21
Output for 5.6.20
1:35<br />5.6.20
Output for 5.6.19
1:35<br />5.6.19
Output for 5.6.18
1:35<br />5.6.18
Output for 5.6.17
1:35<br />5.6.17
Output for 5.6.16
1:35<br />5.6.16
Output for 5.6.15
1:35<br />5.6.15
Output for 5.6.14
1:35<br />5.6.14
Output for 5.6.13
1:35<br />5.6.13
Output for 5.6.12
1:35<br />5.6.12
Output for 5.6.11
1:35<br />5.6.11
Output for 5.6.10
1:35<br />5.6.10
Output for 5.6.9
1:35<br />5.6.9
Output for 5.6.8
1:35<br />5.6.8
Output for 5.6.7
1:35<br />5.6.7
Output for 5.6.6
1:35<br />5.6.6
Output for 5.6.5
1:35<br />5.6.5
Output for 5.6.4
1:35<br />5.6.4
Output for 5.6.3
1:35<br />5.6.3
Output for 5.6.2
1:35<br />5.6.2
Output for 5.6.1
1:35<br />5.6.1
Output for 5.6.0
1:35<br />5.6.0
Output for 5.5.38
1:35<br />5.5.38
Output for 5.5.37
1:35<br />5.5.37
Output for 5.5.36
1:35<br />5.5.36
Output for 5.5.35
1:35<br />5.5.35
Output for 5.5.34
1:35<br />5.5.34
Output for 5.5.33
1:35<br />5.5.33
Output for 5.5.32
1:35<br />5.5.32
Output for 5.5.31
1:35<br />5.5.31
Output for 5.5.30
1:35<br />5.5.30
Output for 5.5.29
1:35<br />5.5.29
Output for 5.5.28
1:35<br />5.5.28
Output for 5.5.27
1:35<br />5.5.27
Output for 5.5.26
1:35<br />5.5.26
Output for 5.5.25
1:35<br />5.5.25
Output for 5.5.24
1:35<br />5.5.24
Output for 5.5.23
1:35<br />5.5.23
Output for 5.5.22
1:35<br />5.5.22
Output for 5.5.21
1:35<br />5.5.21
Output for 5.5.20
1:35<br />5.5.20
Output for 5.5.19
1:35<br />5.5.19
Output for 5.5.18
1:35<br />5.5.18
Output for 5.5.16
1:35<br />5.5.16
Output for 5.5.15
1:35<br />5.5.15
Output for 5.5.14
1:35<br />5.5.14
Output for 5.5.13
1:35<br />5.5.13
Output for 5.5.12
1:35<br />5.5.12
Output for 5.5.11
1:35<br />5.5.11
Output for 5.5.10
1:35<br />5.5.10
Output for 5.5.9
1:35<br />5.5.9
Output for 5.5.8
1:35<br />5.5.8
Output for 5.5.7
1:35<br />5.5.7
Output for 5.5.6
1:35<br />5.5.6
Output for 5.5.5
1:35<br />5.5.5
Output for 5.5.4
1:35<br />5.5.4
Output for 5.5.3
1:35<br />5.5.3
Output for 5.5.2
1:35<br />5.5.2
Output for 5.5.1
1:35<br />5.5.1
Output for 5.5.0
1:35<br />5.5.0
Output for 5.4.45
1:35<br />5.4.45
Output for 5.4.44
1:35<br />5.4.44
Output for 5.4.43
1:35<br />5.4.43
Output for 5.4.42
1:35<br />5.4.42
Output for 5.4.41
1:35<br />5.4.41
Output for 5.4.40
1:35<br />5.4.40
Output for 5.4.39
1:35<br />5.4.39
Output for 5.4.38
1:35<br />5.4.38
Output for 5.4.37
1:35<br />5.4.37
Output for 5.4.36
1:35<br />5.4.36
Output for 5.4.35
1:35<br />5.4.35
Output for 5.4.34
1:35<br />5.4.34
Output for 5.4.32
1:35<br />5.4.32
Output for 5.4.31
1:35<br />5.4.31
Output for 5.4.30
1:35<br />5.4.30
Output for 5.4.29
1:35<br />5.4.29
Output for 5.4.28
1:35<br />5.4.28
Output for 5.4.27
1:35<br />5.4.27
Output for 5.4.26
1:35<br />5.4.26
Output for 5.4.25
1:35<br />5.4.25
Output for 5.4.24
1:35<br />5.4.24
Output for 5.4.23
1:35<br />5.4.23
Output for 5.4.22
1:35<br />5.4.22
Output for 5.4.21
1:35<br />5.4.21
Output for 5.4.20
1:35<br />5.4.20
Output for 5.4.19
1:35<br />5.4.19
Output for 5.4.18
1:35<br />5.4.18
Output for 5.4.17
1:35<br />5.4.17
Output for 5.4.16
1:35<br />5.4.16
Output for 5.4.15
1:35<br />5.4.15
Output for 5.4.14
1:35<br />5.4.14
Output for 5.4.13
1:35<br />5.4.13
Output for 5.4.12
1:35<br />5.4.12
Output for 5.4.11
1:35<br />5.4.11
Output for 5.4.10
1:35<br />5.4.10
Output for 5.4.9
1:35<br />5.4.9
Output for 5.4.8
1:35<br />5.4.8
Output for 5.4.7
1:35<br />5.4.7
Output for 5.4.6
1:35<br />5.4.6
Output for 5.4.5
1:35<br />5.4.5
Output for 5.4.4
1:35<br />5.4.4
Output for 5.4.3
1:35<br />5.4.3
Output for 5.4.2
1:35<br />5.4.2
Output for 5.4.1
1:35<br />5.4.1
Output for 5.4.0
1:35<br />5.4.0
Output for 5.3.29
1:35<br />5.3.29
Output for 5.3.28
1:35<br />5.3.28
Output for 5.3.27
1:35<br />5.3.27
Output for 5.3.26
1:35<br />5.3.26
Output for 5.3.25
1:35<br />5.3.25
Output for 5.3.24
1:35<br />5.3.24
Output for 5.3.23
1:35<br />5.3.23
Output for 5.3.22
1:35<br />5.3.22
Output for 5.3.21
1:35<br />5.3.21
Output for 5.3.20
1:35<br />5.3.20
Output for 5.3.19
1:35<br />5.3.19
Output for 5.3.18
1:35<br />5.3.18
Output for 5.3.17
1:35<br />5.3.17
Output for 5.3.16
1:35<br />5.3.16
Output for 5.3.15
1:35<br />5.3.15
Output for 5.3.14
1:35<br />5.3.14
Output for 5.3.13
1:35<br />5.3.13
Output for 5.3.12
1:35<br />5.3.12
Output for 5.3.11
1:35<br />5.3.11
Output for 5.3.10
1:35<br />5.3.10
Output for 5.3.9
1:35<br />5.3.9
Output for 5.3.8
1:35<br />5.3.8
Output for 5.3.7
1:35<br />5.3.7
Output for 5.3.6
1:35<br />5.3.6
Output for 5.3.5
1:35<br />5.3.5
Output for 5.3.4
1:35<br />5.3.4
Output for 5.3.3
1:35<br />5.3.3
Output for 5.3.2
1:35<br />5.3.2
Output for 5.3.1
1:35<br />5.3.1
Output for 5.3.0
1:35<br />5.3.0