<?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 - 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.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.17 - 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(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 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"
}
preferences:
281.44 ms | 403 KiB | 339 Q