From 7953531802bb28c9d1e28578645e64706b728ea3 Mon Sep 17 00:00:00 2001 From: Bryan Nielsen Date: Fri, 9 Aug 2024 11:03:00 -0400 Subject: [PATCH] Added timezone argument to GraphQL FormattableDate type --- CHANGELOG.md | 4 ++++ src/Api/Graph/Fields/FormattableDate.php | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6db8165..523bcaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Added + +- Timezone argument for GraphQL FormattableDate types + ### Fixed - Bug where integer dates were not handled properly when used as a GraphQL FormattableDate diff --git a/src/Api/Graph/Fields/FormattableDate.php b/src/Api/Graph/Fields/FormattableDate.php index f76b96a..48ed00f 100644 --- a/src/Api/Graph/Fields/FormattableDate.php +++ b/src/Api/Graph/Fields/FormattableDate.php @@ -33,6 +33,11 @@ public function args(): array 'type' => Type::boolean(), 'defaultValue' => false, ], + 'timezone' => [ + 'type' => Type::string(), + 'defaultValue' => null, + 'description' => 'Timezone defaults to UTC. Use "SYSTEM" for the value set in ExpressionEngine', + ], ]; } @@ -48,6 +53,16 @@ protected function resolve($root, array $args): ?string return null; } + if ($args['timezone']) { + if ($args['timezone'] === 'SYSTEM') { + $args['timezone'] = ee()->config->item('default_site_timezone') ?: date_default_timezone_get(); + } + + if (in_array($args['timezone'], \DateTimeZone::listIdentifiers())) { + $date->setTimezone($args['timezone']); + } + } + if ($args['relative']) { return $date->diffForHumans(); }