3v4l.org

run code in 300+ PHP versions simultaneously
<?php $datetime1 = new DateTime('2017-04-26 18:13:06'); $datetime2 = new DateTime('2011-01-17 17:13:00'); // change the millenium to see output difference $diff = $datetime1->diff($datetime2); // this will get you very close, but it will not pad the digits to conform with your expected format echo "Raw Difference: " . $diff->format('%y years %m months %d days %h hours %i minutes %s seconds') . "\n"; // Notice the impact when you change $datetime2's millenium from '1' to '2' echo "Invalid format: " . $diff->format('%Y-%m-%d %H:%i:%s') . "\n"; // only H does it right $details = array_intersect_key((array)$diff,array_flip(['y','m','d','h','i','s'])); echo '$diff = ' . var_export($details, true) . "\n"; printf("Valid format: %04d-%02d-%02d %02d:%02d:%02d", $diff->y, $diff->m, $diff->d, $diff->h, $diff->i, $diff->s); // now all components of datetime are properly padded
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 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.19, 8.3.0 - 8.3.7
Raw Difference: 6 years 3 months 9 days 1 hours 0 minutes 6 seconds Invalid format: 06-3-9 01:0:6 $diff = array ( 'y' => 6, 'm' => 3, 'd' => 9, 'h' => 1, 'i' => 0, 's' => 6, ) Valid format: 0006-03-09 01:00:06
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[', expecting ')' in /in/VFBeK on line 13
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, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[', expecting ')' in /in/VFBeK on line 13
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/VFBeK on line 13
Process exited with code 255.

preferences:
207.93 ms | 401 KiB | 470 Q