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

preferences:
28.41 ms | 407 KiB | 5 Q