3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class MyInterval extends DateInterval { public static function instance(DateInterval $di) { $date = array_filter(array( 'Y' => $di->y, 'M' => $di->m, 'D' => $di->d, )); $time = array_filter(array( 'H' => $di->h, 'M' => $di->i, 'S' => $di->s, )); $specString = 'P'; foreach ($date as $key => $value) { $specString .= $value.$key; } if (count($time) > 0) { $specString .= 'T'; foreach ($time as $key => $value) { $specString .= $value.$key; } } return new static($specString === 'P' ? 'PT0S' : $specString); } } $diff = (new DateTime('yesterday'))->diff(new DateTime('tomorrow')); $myDiff = MyInterval::instance($diff); var_dump($myDiff);
based on M0oXl
Output for 7.1.0 - 7.3.0beta1
object(MyInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }
Output for 7.0.0 - 7.0.27
object(MyInterval)#1 (15) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }
Output for hhvm-3.18.5 - 3.22.0
object(MyInterval)#4 (0) { }
Output for 5.6.0 - 5.6.30
object(MyInterval)#2 (15) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }