3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $base = new DateTime("2015-03-04 00:00:00 Europe/Berlin"); $date = new DateTime("2015-03-06 04:23:56 UTC"); $interval = new DateInterval('P1D'); var_dump(ceilDateTimeByInterval($base, $date, $interval)); function ceilDateTimeByInterval($baseDate, $date, $interval) { $ret = $date; $diff = $date->diff($baseDate); if ($interval->y) { $y = ceil($diff->y / $interval->y) * $interval->y; } if ($interval->m) { $m = ceil($diff->m / $interval->m) * $interval->m; } if ($interval->d) { $d = ceil($diff->d / $interval->d) * $interval->d; } if (isset($y) || isset($m) || isset($d)) { $y = isset($y) ? $y : (int)$date->format('Y'); $m = isset($m) ? $m : (int)$date->format('m'); $d = isset($d) ? $d : (int)$date->format('d'); $ret = ($ret === $date) ? clone $ret : $ret; $ret->setDate($y, $m, $d); } if ($interval->h) { $h = ceil($diff->h / $interval->h) * $interval->h; } if ($interval->i) { $i = ceil($diff->i / $interval->i) * $interval->i; } if ($interval->s) { $s = ceil($diff->s / $interval->s) * $interval->s; } if (isset($h) || isset($i) || isset($s)) { $h = isset($h) ? $h : (int)$date->format('G'); $i = isset($i) ? $i : (int)$date->format('i'); $s = isset($s) ? $s : (int)$date->format('s'); $ret = ($ret === $date) ? clone $ret : $ret; $ret->setTime($h, $i, $s); } return $ret; }
based on gOUmC
Output for 5.4.30 - 7.2.0
object(DateTime)#5 (3) { ["date"]=> string(26) "2015-03-02 04:23:56.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
Output for 5.3.0 - 5.4.29
object(DateTime)#5 (3) { ["date"]=> string(19) "2015-03-02 04:23:56" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
Output for 5.2.0 - 5.2.17
Fatal error: Class 'DateInterval' not found in /in/XDboa on line 5
Process exited with code 255.
Output for 5.0.0 - 5.1.6
Fatal error: Class 'DateTime' not found in /in/XDboa on line 3
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_VARIABLE in /in/XDboa on line 30
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_VARIABLE in /in/XDboa on line 30
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/XDboa on line 30
Process exited with code 255.