3v4l.org

run code in 300+ PHP versions simultaneously
<?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' => null === $m ? sprintf('%04d', $y) : (null === $d ? sprintf('%04d-%02d', $y, $m) : sprintf('%04d-%02d-%02d', $y, $m, $d) ), ); } } 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 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
array(4) { ["year"]=> int(1992) ["month"]=> NULL ["day"]=> NULL ["iso"]=> string(4) "1992" } array(4) { ["year"]=> int(1992) ["month"]=> int(1) ["day"]=> NULL ["iso"]=> string(7) "1992-01" } array(4) { ["year"]=> int(1992) ["month"]=> int(1) ["day"]=> int(4) ["iso"]=> string(10) "1992-01-04" } bool(false)
Output for 4.3.0 - 4.3.2
Notice: Undefined index: year in /in/1EBLA on line 5 array(4) { ["year"]=> int(0) ["month"]=> NULL ["day"]=> NULL ["iso"]=> string(4) "0000" } Notice: Undefined index: year in /in/1EBLA on line 5 array(4) { ["year"]=> int(0) ["month"]=> NULL ["day"]=> NULL ["iso"]=> string(4) "0000" } Notice: Undefined index: year in /in/1EBLA on line 5 array(4) { ["year"]=> int(0) ["month"]=> NULL ["day"]=> NULL ["iso"]=> string(4) "0000" } Notice: Undefined index: year in /in/1EBLA on line 5 array(4) { ["year"]=> int(0) ["month"]=> NULL ["day"]=> NULL ["iso"]=> string(4) "0000" }

preferences:
272.18 ms | 403 KiB | 460 Q