From 927fe9d60c06869969737784978559317e7acc49 Mon Sep 17 00:00:00 2001 From: Philippe Date: Tue, 9 Jul 2024 13:52:58 +0200 Subject: [PATCH] Give an option to set a date at midnight --- src/Casts/DateTimeInterfaceCast.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Casts/DateTimeInterfaceCast.php b/src/Casts/DateTimeInterfaceCast.php index 4003d01e..71936855 100644 --- a/src/Casts/DateTimeInterfaceCast.php +++ b/src/Casts/DateTimeInterfaceCast.php @@ -14,7 +14,8 @@ public function __construct( protected null|string|array $format = null, protected ?string $type = null, protected ?string $setTimeZone = null, - protected ?string $timeZone = null + protected ?string $timeZone = null, + protected ?bool $setMidnight = false ) { } @@ -54,7 +55,12 @@ protected function castValue( $this->setTimeZone ??= config('data.date_timezone'); if ($this->setTimeZone) { - return $datetime->setTimezone(new DateTimeZone($this->setTimeZone)); + $datetime = $datetime->setTimezone(new DateTimeZone($this->setTimeZone)); + } + + //Force the time to 00:00:00.000 + if ($this->setMidnight) { + $datetime = $datetime->setTime(0, 0); } return $datetime;