diff --git a/src/Fieldtypes/Date.php b/src/Fieldtypes/Date.php index 3d27b63919..c5c6274db1 100644 --- a/src/Fieldtypes/Date.php +++ b/src/Fieldtypes/Date.php @@ -228,6 +228,17 @@ private function processSingle($data) return null; } + // If the value is an array, the CP has submitted the date and time separately. + // We'll combine them into a single string before parsing, since Carbon can't + // parse an array. A missing date means the field was left empty. + if (is_array($data)) { + if (! ($data['date'] ?? null)) { + return null; + } + + $data = $data['date'].' '.(($data['time'] ?? null) ?: '00:00'); + } + return $this->processDateTime($data); } diff --git a/tests/Fieldtypes/DateTest.php b/tests/Fieldtypes/DateTest.php index b0050887be..2ac9cd4d7b 100644 --- a/tests/Fieldtypes/DateTest.php +++ b/tests/Fieldtypes/DateTest.php @@ -244,6 +244,24 @@ public static function processProvider() ['start' => '2012-08-29', 'end' => '2013-09-27'], ['start' => '2012-08-29', 'end' => '2013-09-27'], ], + 'single with date and time array from cp' => [ + 'UTC', + [], + ['date' => '2012-08-29', 'time' => '13:43'], + '2012-08-29 13:43', + ], + 'single with date array and no time' => [ + 'UTC', + [], + ['date' => '2012-08-29', 'time' => null], + '2012-08-29 00:00', + ], + 'single with empty date array' => [ + 'UTC', + [], + ['date' => null, 'time' => null], + null, + ], ]; }