Update convert

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3522 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2017-04-13 12:17:57 +00:00
parent 001c2a041a
commit 267c260a0f
2 changed files with 13 additions and 7 deletions

View File

@@ -8,8 +8,8 @@
class convert
{
/** Convert Date received in one format to another.
* If the provided string is not corresponding to the format, don't change
* anything.
* If the provided string is not corresponding to the format, generate an
* exception or return the original string
* Format used http://php.net/manual/en/datetime.createfromformat.php
* Do not accept the locale ! The language of the dates is always in english
* @param string $inputDate The date to modify
@@ -20,12 +20,18 @@ class convert
* @return string
*/
public static function convertDate ($inputDate, $inputFormat, $outputFormat,
$exception = false)
$exception = true)
{
if (! is_string ($inputDate))
throw new \Exception ("The date to convert is not a string", 500);
if (! is_string ($inputFormat))
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);
if ($date === false)
{
if ($exception !== false)
if ($exception === true)
throw new \Exception (
"Invalid date provided or not matching the format", 500);
return $inputDate;
@@ -33,7 +39,7 @@ class convert
$errors = $date->getLastErrors();
if ($errors["warning_count"] > 0 || $errors["error_count"] > 0)
{
if ($exception !== false)
if ($exception === true)
throw new \Exception (
"Invalid date provided or not matching the format", 500);
return $inputDate;