add expanded values to dates

This commit is contained in:
Thomas Portelange 2024-04-08 17:11:46 +02:00 committed by GitHub
parent e3c075b7a8
commit f38931f0b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 2 deletions

View File

@ -513,8 +513,8 @@ class DateField extends TextField
* Convert frontend date to the internal representation (ISO 8601).
* The frontend date is also in ISO 8601 when $html5=true.
*
* @param string $date
* @return string The formatted date, or null if not a valid date
* @param ?string $date
* @return ?string The formatted date, or null if not a valid date
*/
protected function frontendToInternal($date)
{
@ -524,6 +524,17 @@ class DateField extends TextField
$fromFormatter = $this->getFrontendFormatter();
$toFormatter = $this->getInternalFormatter();
$timestamp = $fromFormatter->parse($date);
// Retry with expanded value
if ($timestamp === false) {
$zeroFormat = $fromFormatter->format(strtotime(date('Y-01-01 00:00:00')));
$expectedLength = strlen($zeroFormat);
if (strlen($date) < $expectedLength) {
$expandedValue = $date . substr($zeroFormat, strlen($date));
$timestamp = $fromFormatter->parse($expandedValue);
}
}
if ($timestamp === false) {
return null;
}