From e583e9dd9461e546176b42d5b1124860775915cd Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Mon, 7 Sep 2020 11:45:28 +0000 Subject: [PATCH] convert : date allow now to set the input and output timezone git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@6089 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- convert.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/convert.php b/convert.php index 31751c0..a0a101c 100644 --- a/convert.php +++ b/convert.php @@ -17,10 +17,16 @@ class convert * @param string $outputFormat The output format of the date * @param boolean|null $exception If set, generate an exception if the * provided date is invalid + * @param string|null $inputTimezone The timezone used to read the input + * By default, Europe/Paris + * @param string|null $outputTimezone The timezone used to write the output + * By default, Europe/Paris * @return string */ public static function convertDate ($inputDate, $inputFormat, $outputFormat, - $exception = true) + $exception = true, + $inputTimezone = "Europe/Paris", + $outputTimezone = "Europe/Paris") // {{{ { if (! is_string ($inputDate)) @@ -29,7 +35,8 @@ class convert throw new \Exception ("The convert input format is not a string", 500); if (! is_string ($outputFormat)) throw new \Exception ("The convert output format is not a string", 500); - $date = \DateTime::CreateFromFormat ($inputFormat, $inputDate); + $date = \DateTime::CreateFromFormat ($inputFormat, $inputDate, + new \DateTimeZone ($inputTimezone)); if ($date === false) { if ($exception === true) @@ -45,6 +52,8 @@ class convert "Invalid date provided or not matching the format", 500); return $inputDate; } + + $date->setTimezone(new \DateTimeZone ($outputTimezone)); return $date->format ($outputFormat); } // }}}