Update convert
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3522 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
16
convert.php
16
convert.php
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user