<?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;
}
- Output for 5.4.30 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- 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.3.29, 5.4.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.0.5, 5.1.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.3.11, 4.4.0 - 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.
preferences:
258.39 ms | 401 KiB | 310 Q