Wrap the date_helper.php functions with try-catch blocks

This commit is contained in:
Alex Tselegidis 2023-02-20 08:57:50 +01:00
parent 270c261a58
commit 98976ee382
1 changed files with 47 additions and 21 deletions

View File

@ -78,6 +78,8 @@ if ( ! function_exists('format_date'))
* @throws Exception
*/
function format_date(DateTimeInterface|string $value): string
{
try
{
$value_date_time = $value;
@ -88,6 +90,13 @@ if ( ! function_exists('format_date'))
return $value_date_time->format(get_date_format());
}
catch (Exception $e)
{
log_message('error', 'Invalid date provided to the "format_date" helper function: ' . $e->getMessage());
return 'Invalid Date';
}
}
}
if ( ! function_exists('format_time'))
@ -101,7 +110,9 @@ if ( ! function_exists('format_time'))
*
* @throws Exception
*/
function format_date(DateTimeInterface|string $value): string
function format_time(DateTimeInterface|string $value): string
{
try
{
$value_date_time = $value;
@ -112,6 +123,13 @@ if ( ! function_exists('format_time'))
return $value_date_time->format(get_time_format());
}
catch (Exception $e)
{
log_message('error', 'Invalid date provided to the format_time helper function: ' . $e->getMessage());
return 'Invalid Time';
}
}
}
if ( ! function_exists('format_date_time'))
@ -122,20 +140,28 @@ if ( ! function_exists('format_date_time'))
* @param DateTimeInterface|string $value
*
* @return string
*
* @throws Exception
*/
function format_date_time(DateTimeInterface|string $value): string
{
try
{
$value_date_time = $value;
if (is_string($value_date_time))
{
$value_date_time = new DateTime($value);
}
return $value_date_time->format(get_date_time_format());
}
catch (Exception $e)
{
log_message('error', 'Invalid date provided to the format_date_time helper function: ' . $e->getMessage());
return 'Invalid Date-Time';
}
}
}