Add the date_helper.php to the project

This commit is contained in:
Alex Tselegidis 2023-02-20 08:54:29 +01:00
parent fc231fe11f
commit 270c261a58
2 changed files with 142 additions and 0 deletions

View file

@ -69,6 +69,7 @@ $autoload['helper'] = [
'array',
'asset',
'config',
'date',
'debug',
'env',
'file',

View file

@ -0,0 +1,141 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @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());
}
}