From 270c261a583f485c0a6f042a9d255217b96042f4 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Mon, 20 Feb 2023 08:54:29 +0100 Subject: [PATCH] Add the date_helper.php to the project --- application/config/autoload.php | 1 + application/helpers/date_helper.php | 141 ++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 application/helpers/date_helper.php diff --git a/application/config/autoload.php b/application/config/autoload.php index 4f4042bc..c9587ef9 100644 --- a/application/config/autoload.php +++ b/application/config/autoload.php @@ -69,6 +69,7 @@ $autoload['helper'] = [ 'array', 'asset', 'config', + 'date', 'debug', 'env', 'file', diff --git a/application/helpers/date_helper.php b/application/helpers/date_helper.php new file mode 100644 index 00000000..364326a1 --- /dev/null +++ b/application/helpers/date_helper.php @@ -0,0 +1,141 @@ + + * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.4.0 + * ---------------------------------------------------------------------------- */ + +if ( ! function_exists('get_date_format')) +{ + /** + * Get the date format based on the current settings. + * + * @return string + */ + function get_date_format(): string + { + $date_format = setting('date_format'); + + return match ($date_format) + { + 'DMY' => 'd/m/Y', + 'MDY' => 'm/d/Y', + 'YMD' => 'Y/m/d', + default => throw new RuntimeException('Invalid date format value: ' . $date_format), + }; + } +} + +if ( ! function_exists('get_time_format')) +{ + /** + * Get the time format based on the current settings. + * + * @return string + */ + function get_time_format(): string + { + $time_format = setting('time_format'); + + return match ($time_format) + { + 'military' => 'H:i', + 'regular' => 'g:i a', + default => throw new RuntimeException('Invalid time format value: ' . $time_format), + }; + } +} + +if ( ! function_exists('get_date_time_format')) +{ + /** + * Get the date-time format based on the current settings. + * + * @return string + */ + function get_date_time_format(): string + { + return get_date_format() . ' ' . get_time_format(); + } +} + + +if ( ! function_exists('format_date')) +{ + /** + * Format a date string based on the current app settings. + * + * @param DateTimeInterface|string $value + * + * @return string + * + * @throws Exception + */ + function format_date(DateTimeInterface|string $value): string + { + $value_date_time = $value; + + if (is_string($value_date_time)) + { + $value_date_time = new DateTime($value); + } + + return $value_date_time->format(get_date_format()); + } +} + +if ( ! function_exists('format_time')) +{ + /** + * Format a time string based on the current app settings. + * + * @param DateTimeInterface|string $value + * + * @return string + * + * @throws Exception + */ + function format_date(DateTimeInterface|string $value): string + { + $value_date_time = $value; + + if (is_string($value_date_time)) + { + $value_date_time = new DateTime($value); + } + + return $value_date_time->format(get_time_format()); + } +} + +if ( ! function_exists('format_date_time')) +{ + /** + * Format a time string based on the current app settings. + * + * @param DateTimeInterface|string $value + * + * @return string + * + * @throws Exception + */ + function format_date_time(DateTimeInterface|string $value): string + { + $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()); + } +} + +