diff --git a/config/data.php b/config/data.php index 2265e524a..d91cbfa69 100644 --- a/config/data.php +++ b/config/data.php @@ -10,6 +10,11 @@ */ 'date_format' => DATE_ATOM, + /** + * Set a default timezone for casting date + */ + 'default_timezone' => null, + /** * It is possible to enable certain features of the package, these would otherwise * be breaking changes, and thus they are disabled by default. In the next major diff --git a/src/Casts/DateTimeInterfaceCast.php b/src/Casts/DateTimeInterfaceCast.php index 5231e0271..bcb9c410d 100644 --- a/src/Casts/DateTimeInterfaceCast.php +++ b/src/Casts/DateTimeInterfaceCast.php @@ -51,6 +51,8 @@ protected function castValue( throw CannotCastDate::create($formats->toArray(), $type, $value); } + $this->setTimeZone ??= config('data.default_timezone'); + if ($this->setTimeZone) { return $datetime->setTimezone(new DateTimeZone($this->setTimeZone)); } diff --git a/src/Transformers/DateTimeInterfaceTransformer.php b/src/Transformers/DateTimeInterfaceTransformer.php index 5781d3b6f..e8867fc97 100644 --- a/src/Transformers/DateTimeInterfaceTransformer.php +++ b/src/Transformers/DateTimeInterfaceTransformer.php @@ -21,6 +21,8 @@ public function __construct( public function transform(DataProperty $property, mixed $value, TransformationContext $context): string { + $this->setTimeZone ??= config('data.default_timezone'); + /** @var DateTimeInterface $value */ if ($this->setTimeZone) { $value = (clone $value)->setTimezone(new DateTimeZone($this->setTimeZone));