3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class MyDateTime extends DateTime { public static function createFromFormat($dformat, $dvalue) { $schedule = $dvalue; $schedule_format = str_replace(array('Y','m','d', 'H', 'i','a'),array('%Y','%m','%d', '%I', '%M', '%p' ) ,$dformat); // %Y, %m and %d correspond to date()'s Y m and d. // %I corresponds to H, %M to i and %p to a $ugly = strptime($schedule, $schedule_format); $ymd = sprintf( // This is a format string that takes six total decimal // arguments, then left-pads them with zeros to either // 4 or 2 characters, as needed '%04d-%02d-%02d %02d:%02d:%02d', $ugly['tm_year'] + 1900, // This will be "111", so we need to add 1900. $ugly['tm_mon'] + 1, // This will be the month minus one, so we add one. $ugly['tm_mday'], $ugly['tm_hour'], $ugly['tm_min'], $ugly['tm_sec'] ); $new_schedule = new MyDateTime($ymd); return $new_schedule; } public function getTimestamp() { return method_exists('DateTime', 'getTimestamp') ? parent::getTimestamp() : $this->format('U'); } } $dateTime = MyDateTime::createFromFormat('d/m/Y', '13/06/2013'); var_dump($dateTime); var_dump($dateTime->format('Y-m-d')); var_dump($dateTime->getTimestamp());
based on Fu7md
Output for 7.0.0 - 7.1.0
Warning: Declaration of MyDateTime::createFromFormat($dformat, $dvalue) should be compatible with DateTime::createFromFormat($format, $time, $object = NULL) in /in/JYOC8 on line 29 object(MyDateTime)#1 (3) { ["date"]=> string(26) "2013-06-13 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } string(10) "2013-06-13" int(1371074400)
Output for hhvm-3.10.0 - 3.12.0
object(MyDateTime)#1 (3) { ["date"]=> string(26) "2013-06-13 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } string(10) "2013-06-13" int(1371074400)
Output for 5.4.30 - 5.6.28
Strict Standards: Declaration of MyDateTime::createFromFormat() should be compatible with DateTime::createFromFormat($format, $time, $object = NULL) in /in/JYOC8 on line 29 object(MyDateTime)#1 (3) { ["date"]=> string(26) "2013-06-13 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } string(10) "2013-06-13" int(1371074400)
Output for 5.4.0 - 5.4.29
Strict Standards: Declaration of MyDateTime::createFromFormat() should be compatible with DateTime::createFromFormat($format, $time, $object = NULL) in /in/JYOC8 on line 29 object(MyDateTime)#1 (3) { ["date"]=> string(19) "2013-06-13 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } string(10) "2013-06-13" int(1371074400)
Output for 5.3.0 - 5.3.29
Strict Standards: Declaration of MyDateTime::createFromFormat() should be compatible with that of DateTime::createFromFormat() in /in/JYOC8 on line 29 object(MyDateTime)#1 (3) { ["date"]=> string(19) "2013-06-13 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } string(10) "2013-06-13" int(1371074400)
Output for 5.2.0 - 5.2.17
object(MyDateTime)#1 (0) { } string(10) "2013-06-13" string(10) "1371074400"
Output for 5.0.0 - 5.1.6
Fatal error: Class 'DateTime' not found in /in/JYOC8 on line 2
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/JYOC8 on line 3
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_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/JYOC8 on line 3
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/JYOC8 on line 3
Process exited with code 255.