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
This commit is contained in:
13
convert.php
13
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);
|
||||
}
|
||||
// }}}
|
||||
|
||||
Reference in New Issue
Block a user