3v4l.org

run code in 150+ 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 'Original time:- ' . $minute->format('Y-m-d H:i') . "\n"; echo 'Rounded time :- ' . roundDateTime($minute)->format('Y-m-d H:i') . "\n\n"; }
Output for 5.3.4 - 7.1.0
Original time:- 2016-05-05 09:24 Rounded time :- 2016-05-05 09:00 Original time:- 2016-05-05 09:25 Rounded time :- 2016-05-05 09:00 Original time:- 2016-05-05 09:26 Rounded time :- 2016-05-05 09:00 Original time:- 2016-05-05 09:27 Rounded time :- 2016-05-05 09:00 Original time:- 2016-05-05 09:28 Rounded time :- 2016-05-05 09:00 Original time:- 2016-05-05 09:29 Rounded time :- 2016-05-05 09:00 Original time:- 2016-05-05 09:30 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:31 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:32 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:33 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:34 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:35 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:36 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:37 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:38 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:39 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:40 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:41 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:42 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:43 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:44 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:45 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:46 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:47 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:48 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:49 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:50 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:51 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:52 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:53 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:54 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:55 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:56 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:57 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:58 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:59 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 10:00 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:01 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:02 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:03 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:04 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:05 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:06 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:07 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:08 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:09 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:10 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:11 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:12 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:13 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:14 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:15 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:16 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:17 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:18 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:19 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:20 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:21 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:22 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:23 Rounded time :- 2016-05-05 10:00
Output for 5.3.0 - 5.3.3
Original time:- 2016-05-05 09:24 Rounded time :- 2016-05-05 09:00 Original time:- 2016-05-05 09:25 Rounded time :- 2016-05-05 09:00 Original time:- 2016-05-05 09:26 Rounded time :- 2016-05-05 09:00 Original time:- 2016-05-05 09:27 Rounded time :- 2016-05-05 09:00 Original time:- 2016-05-05 09:28 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:29 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:30 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:31 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:32 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:33 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:34 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:35 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:36 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:37 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:38 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:39 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:40 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:41 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:42 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:43 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:44 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:45 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:46 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:47 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:48 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:49 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:50 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:51 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:52 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:53 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:54 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:55 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:56 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:57 Rounded time :- 2016-05-05 09:30 Original time:- 2016-05-05 09:58 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 09:59 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:00 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:01 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:02 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:03 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:04 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:05 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:06 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:07 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:08 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:09 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:10 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:11 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:12 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:13 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:14 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:15 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:16 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:17 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:18 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:19 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:20 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:21 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:22 Rounded time :- 2016-05-05 10:00 Original time:- 2016-05-05 10:23 Rounded time :- 2016-05-05 10:00
Output for 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/VFgdF on line 7 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/VFgdF on line 16 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/VFgdF on line 17 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/VFgdF on line 18 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/VFgdF on line 19 Fatal error: Class 'DateInterval' not found in /in/VFgdF 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/VFgdF on line 7 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/VFgdF on line 16 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/VFgdF on line 17 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/VFgdF on line 18 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/VFgdF on line 19 Fatal error: Class 'DateTime' not found in /in/VFgdF 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/VFgdF on line 7 Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/VFgdF 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/VFgdF on line 7 Parse error: parse error, unexpected T_STRING, expecting ')' in /in/VFgdF 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/VFgdF on line 7 Parse error: parse error, expecting `')'' in /in/VFgdF on line 7
Process exited with code 255.