3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class DateTimeFactory { const DATE_INTERVAL = 'PT20M'; const DATE_INTERVAL_FOR_METHOD_3 = '+20 minutes'; // I personally prefer adding DateInterval public static function createDateTimeByInterval1(DateInterval $dateInterval) { // create DateTime, add Date and/or TimeZone in constructor if needed. $dateTime = new DateTime(); return $dateTime->add($dateInterval); } // Using a ISO 8601 String public static function createDateTimeByInterval2($intervalStr) { // create DateTime, add Date and/or TimeZone in constructor if needed. $dateTime = new DateTime(); return $dateTime->add(new DateInterval($intervalStr)); } // Using modify public static function createDateTimeByInterval3($intervalStr) { // create DateTime, add Date and/or TimeZone in constructor if needed. $dateTime = new DateTime(); return $dateTime->modify($intervalStr); } } var_dump( DateTimeFactory::createDateTimeByInterval1(new DateInterval(DateTimeFactory::DATE_INTERVAL)), DateTimeFactory::createDateTimeByInterval2(DateTimeFactory::DATE_INTERVAL), DateTimeFactory::createDateTimeByInterval3(DateTimeFactory::DATE_INTERVAL_FOR_METHOD_3) );
Output for 7.1.0
object(DateTime)#2 (3) { ["date"]=> string(26) "2014-01-12 13:25:04.004722" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } object(DateTime)#1 (3) { ["date"]=> string(26) "2014-01-12 13:25:04.004730" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } object(DateTime)#3 (3) { ["date"]=> string(26) "2014-01-12 13:25:04.004735" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 5.4.30 - 5.4.45, 5.5.14 - 7.0.14
object(DateTime)#2 (3) { ["date"]=> string(26) "2014-01-12 13:25:04.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } object(DateTime)#1 (3) { ["date"]=> string(26) "2014-01-12 13:25:04.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } object(DateTime)#3 (3) { ["date"]=> string(26) "2014-01-12 13:25:04.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 5.3.0 - 5.4.29, 5.5.0 - 5.5.13
object(DateTime)#2 (3) { ["date"]=> string(19) "2014-01-12 13:25:04" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } object(DateTime)#1 (3) { ["date"]=> string(19) "2014-01-12 13:25:04" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } object(DateTime)#3 (3) { ["date"]=> string(19) "2014-01-12 13:25:04" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 5.0.0 - 5.2.17
Fatal error: Class 'DateInterval' not found in /in/JnGR2 on line 34
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/JnGR2 on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/JnGR2 on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/JnGR2 on line 5
Process exited with code 255.