3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * @param DateTime $dateTime The dateTime to be rounded * * @return DateTime The rounded DateTime */ function roundDateTime(\DateTime $dateTime) { $rounded = clone $dateTime; //Avoid side effects return $rounded->setTimestamp($rounded->getTimestamp() - $rounded->getTimestamp() % 1800); return $rounded; } // Test code below $start = new \DateTime(); $end = new \DateTime('+ 1 hour'); $interval = new \DateInterval('PT1M'); $period = new \DatePeriod($start, $interval, $end); foreach($period as $minute){ echo $minute->format('Y-m-d H:i') . "\n"; echo roundDateTime($minute)->format('Y-m-d H:i') . "\n\n"; }
Output for 5.3.4 - 5.6.38, hhvm-3.10.1 - 3.22.0, 7.0.0 - 7.3.0rc4
2014-05-17 21:02 2014-05-17 21:00 2014-05-17 21:03 2014-05-17 21:00 2014-05-17 21:04 2014-05-17 21:00 2014-05-17 21:05 2014-05-17 21:00 2014-05-17 21:06 2014-05-17 21:00 2014-05-17 21:07 2014-05-17 21:00 2014-05-17 21:08 2014-05-17 21:00 2014-05-17 21:09 2014-05-17 21:00 2014-05-17 21:10 2014-05-17 21:00 2014-05-17 21:11 2014-05-17 21:00 2014-05-17 21:12 2014-05-17 21:00 2014-05-17 21:13 2014-05-17 21:00 2014-05-17 21:14 2014-05-17 21:00 2014-05-17 21:15 2014-05-17 21:00 2014-05-17 21:16 2014-05-17 21:00 2014-05-17 21:17 2014-05-17 21:00 2014-05-17 21:18 2014-05-17 21:00 2014-05-17 21:19 2014-05-17 21:00 2014-05-17 21:20 2014-05-17 21:00 2014-05-17 21:21 2014-05-17 21:00 2014-05-17 21:22 2014-05-17 21:00 2014-05-17 21:23 2014-05-17 21:00 2014-05-17 21:24 2014-05-17 21:00 2014-05-17 21:25 2014-05-17 21:00 2014-05-17 21:26 2014-05-17 21:00 2014-05-17 21:27 2014-05-17 21:00 2014-05-17 21:28 2014-05-17 21:00 2014-05-17 21:29 2014-05-17 21:00 2014-05-17 21:30 2014-05-17 21:30 2014-05-17 21:31 2014-05-17 21:30 2014-05-17 21:32 2014-05-17 21:30 2014-05-17 21:33 2014-05-17 21:30 2014-05-17 21:34 2014-05-17 21:30 2014-05-17 21:35 2014-05-17 21:30 2014-05-17 21:36 2014-05-17 21:30 2014-05-17 21:37 2014-05-17 21:30 2014-05-17 21:38 2014-05-17 21:30 2014-05-17 21:39 2014-05-17 21:30 2014-05-17 21:40 2014-05-17 21:30 2014-05-17 21:41 2014-05-17 21:30 2014-05-17 21:42 2014-05-17 21:30 2014-05-17 21:43 2014-05-17 21:30 2014-05-17 21:44 2014-05-17 21:30 2014-05-17 21:45 2014-05-17 21:30 2014-05-17 21:46 2014-05-17 21:30 2014-05-17 21:47 2014-05-17 21:30 2014-05-17 21:48 2014-05-17 21:30 2014-05-17 21:49 2014-05-17 21:30 2014-05-17 21:50 2014-05-17 21:30 2014-05-17 21:51 2014-05-17 21:30 2014-05-17 21:52 2014-05-17 21:30 2014-05-17 21:53 2014-05-17 21:30 2014-05-17 21:54 2014-05-17 21:30 2014-05-17 21:55 2014-05-17 21:30 2014-05-17 21:56 2014-05-17 21:30 2014-05-17 21:57 2014-05-17 21:30 2014-05-17 21:58 2014-05-17 21:30 2014-05-17 21:59 2014-05-17 21:30 2014-05-17 22:00 2014-05-17 22:00 2014-05-17 22:01 2014-05-17 22:00
Output for 5.3.0 - 5.3.3
2014-05-17 21:02 2014-05-17 21:00 2014-05-17 21:03 2014-05-17 21:00 2014-05-17 21:04 2014-05-17 21:00 2014-05-17 21:05 2014-05-17 21:00 2014-05-17 21:06 2014-05-17 21:00 2014-05-17 21:07 2014-05-17 21:00 2014-05-17 21:08 2014-05-17 21:00 2014-05-17 21:09 2014-05-17 21:00 2014-05-17 21:10 2014-05-17 21:00 2014-05-17 21:11 2014-05-17 21:00 2014-05-17 21:12 2014-05-17 21:00 2014-05-17 21:13 2014-05-17 21:00 2014-05-17 21:14 2014-05-17 21:00 2014-05-17 21:15 2014-05-17 21:00 2014-05-17 21:16 2014-05-17 21:00 2014-05-17 21:17 2014-05-17 21:00 2014-05-17 21:18 2014-05-17 21:00 2014-05-17 21:19 2014-05-17 21:00 2014-05-17 21:20 2014-05-17 21:00 2014-05-17 21:21 2014-05-17 21:00 2014-05-17 21:22 2014-05-17 21:00 2014-05-17 21:23 2014-05-17 21:00 2014-05-17 21:24 2014-05-17 21:00 2014-05-17 21:25 2014-05-17 21:00 2014-05-17 21:26 2014-05-17 21:00 2014-05-17 21:27 2014-05-17 21:00 2014-05-17 21:28 2014-05-17 21:30 2014-05-17 21:29 2014-05-17 21:30 2014-05-17 21:30 2014-05-17 21:30 2014-05-17 21:31 2014-05-17 21:30 2014-05-17 21:32 2014-05-17 21:30 2014-05-17 21:33 2014-05-17 21:30 2014-05-17 21:34 2014-05-17 21:30 2014-05-17 21:35 2014-05-17 21:30 2014-05-17 21:36 2014-05-17 21:30 2014-05-17 21:37 2014-05-17 21:30 2014-05-17 21:38 2014-05-17 21:30 2014-05-17 21:39 2014-05-17 21:30 2014-05-17 21:40 2014-05-17 21:30 2014-05-17 21:41 2014-05-17 21:30 2014-05-17 21:42 2014-05-17 21:30 2014-05-17 21:43 2014-05-17 21:30 2014-05-17 21:44 2014-05-17 21:30 2014-05-17 21:45 2014-05-17 21:30 2014-05-17 21:46 2014-05-17 21:30 2014-05-17 21:47 2014-05-17 21:30 2014-05-17 21:48 2014-05-17 21:30 2014-05-17 21:49 2014-05-17 21:30 2014-05-17 21:50 2014-05-17 21:30 2014-05-17 21:51 2014-05-17 21:30 2014-05-17 21:52 2014-05-17 21:30 2014-05-17 21:53 2014-05-17 21:30 2014-05-17 21:54 2014-05-17 21:30 2014-05-17 21:55 2014-05-17 21:30 2014-05-17 21:56 2014-05-17 21:30 2014-05-17 21:57 2014-05-17 21:30 2014-05-17 21:58 2014-05-17 22:00 2014-05-17 21:59 2014-05-17 22:00 2014-05-17 22:00 2014-05-17 22:00 2014-05-17 22:01 2014-05-17 22:00
Output for 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 7 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 16 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 17 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 18 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 19 Fatal error: Class 'DateInterval' not found in /in/Xvs2v on line 18
Process exited with code 255.
Output for 5.0.0 - 5.1.6
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 7 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 16 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 17 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 18 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 19 Fatal error: Class 'DateTime' not found in /in/Xvs2v on line 16
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 7 Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/Xvs2v on line 7
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 7 Parse error: parse error, unexpected T_STRING, expecting ')' in /in/Xvs2v on line 7
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Xvs2v on line 7 Parse error: parse error, expecting `')'' in /in/Xvs2v on line 7
Process exited with code 255.