Skip to content

Commit

Permalink
Default to YYY-MM-DD format when converting date input value to strin…
Browse files Browse the repository at this point in the history
…g to be comparable when sorting and filtering
  • Loading branch information
giuscris committed Mar 2, 2025
1 parent b75b496 commit a94b5e2
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions formwork/fields/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@

return function (App $app): array {
return [
'format' => function (Field $field, ?string $format = null, string $type = 'pattern') use ($app): string {
$format ??= $app->config()->get('system.date.dateFormat');
/**
* By default the date is formatted using the YYYY-MM-DD format.
* This is at the same time human-readable and comparable when sorting.
*/
'format' => function (Field $field, string $format = 'YYYY-MM-DD', string $type = 'pattern') use ($app): string {
$translation = $app->translations()->getCurrent();

if ($format !== null) {
$format = match (strtolower($type)) {
'pattern' => Date::patternToFormat($format),
'date' => $format,
default => throw new InvalidArgumentException('Invalid date format type'),
};
}
$format = match (strtolower($type)) {
'pattern' => Date::patternToFormat($format),
'date' => $format,
default => throw new InvalidArgumentException('Invalid date format type'),
};

return $field->isEmpty() ? '' : Date::formatTimestamp($field->toTimestamp(), $format, $translation);
},

Expand Down

0 comments on commit a94b5e2

Please sign in to comment.