3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function scholar_parse_date($date) { if (preg_match('/^\s*(?P<year>\d+)(?>-(?P<month>\d{1,2}))?(?>-(?P<day>\d{1,2}))?\s*$/', $date, $match)) { $y = intval($match['year']); $m = isset($match['month']) ? intval($match['month']) : null; $d = isset($match['day']) ? intval($match['day']) : null; // Jezeli $m jest nullem, tzn. ze $d tez jest nullem, wiec // rok jako liczba calkowita jest poprawny. // Jezeli $m nie jest nullem, musi byc z przedzialu 1..12. // Jezeli $d jest nullem, nic wiecej nie trzeba sprawdzac, // jezeli nie jest, trzeba sprawdzic pelna date. $valid = ((null === $m) || (1 <= $m && $m <= 12)) && ((null === $d) || checkdate($m, $d, $y)); if ($valid) { return array( 'year' => $y, 'month' => $m, 'day' => $d, 'iso' => sprintf('%04d-%02d-%02d', $y, $m, $d), // ISO 8601 ); } } return false; } var_dump(scholar_parse_date('1992')); var_dump(scholar_parse_date('1992-01')); var_dump(scholar_parse_date('1992-01-04')); var_dump(scholar_parse_date('1992-01-32'));
Output for 4.3.3 - 5.6.28, hhvm-3.12.0, 7.0.0 - 7.1.0
array(4) { ["year"]=> int(1992) ["month"]=> NULL ["day"]=> NULL ["iso"]=> string(10) "1992-00-00" } array(4) { ["year"]=> int(1992) ["month"]=> int(1) ["day"]=> NULL ["iso"]=> string(10) "1992-01-00" } array(4) { ["year"]=> int(1992) ["month"]=> int(1) ["day"]=> int(4) ["iso"]=> string(10) "1992-01-04" } bool(false)
Output for hhvm-3.10.0

Process exited with code 153.
Output for 4.3.0 - 4.3.2
Notice: Undefined index: year in /in/832Lv on line 5 array(4) { ["year"]=> int(0) ["month"]=> NULL ["day"]=> NULL ["iso"]=> string(10) "0000-00-00" } Notice: Undefined index: year in /in/832Lv on line 5 array(4) { ["year"]=> int(0) ["month"]=> NULL ["day"]=> NULL ["iso"]=> string(10) "0000-00-00" } Notice: Undefined index: year in /in/832Lv on line 5 array(4) { ["year"]=> int(0) ["month"]=> NULL ["day"]=> NULL ["iso"]=> string(10) "0000-00-00" } Notice: Undefined index: year in /in/832Lv on line 5 array(4) { ["year"]=> int(0) ["month"]=> NULL ["day"]=> NULL ["iso"]=> string(10) "0000-00-00" }