MaketRandevu/application/helpers/date_helper.php

163 lines
4.3 KiB
PHP
Raw Normal View History

2023-02-20 10:54:29 +03:00
<?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')) {
2023-02-20 10:54:29 +03:00
/**
* 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) {
2023-02-20 10:54:29 +03:00
'DMY' => 'd/m/Y',
'MDY' => 'm/d/Y',
'YMD' => 'Y/m/d',
default => throw new RuntimeException('Invalid date format value: ' . $date_format)
2023-02-20 10:54:29 +03:00
};
}
}
if (!function_exists('get_time_format')) {
2023-02-20 10:54:29 +03:00
/**
* 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) {
2023-02-20 10:54:29 +03:00
'military' => 'H:i',
'regular' => 'g:i a',
default => throw new RuntimeException('Invalid time format value: ' . $time_format)
2023-02-20 10:54:29 +03:00
};
}
}
if (!function_exists('get_date_time_format')) {
2023-02-20 10:54:29 +03:00
/**
* 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')) {
2023-02-20 10:54:29 +03:00
/**
* 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
{
try {
$value_date_time = $value;
if (is_string($value_date_time)) {
$value_date_time = new DateTime($value);
}
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());
2023-02-20 10:54:29 +03:00
return 'Invalid Date';
}
2023-02-20 10:54:29 +03:00
}
}
if (!function_exists('format_time')) {
2023-02-20 10:54:29 +03:00
/**
* Format a time string based on the current app settings.
*
* @param DateTimeInterface|string $value
*
* @return string
*
* @throws Exception
*/
function format_time(DateTimeInterface|string $value): string
2023-02-20 10:54:29 +03:00
{
try {
$value_date_time = $value;
if (is_string($value_date_time)) {
$value_date_time = new DateTime($value);
}
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());
2023-02-20 10:54:29 +03:00
return 'Invalid Time';
}
2023-02-20 10:54:29 +03:00
}
}
if (!function_exists('format_date_time')) {
2023-02-20 10:54:29 +03:00
/**
* Format a time string based on the current app settings.
*
* @param DateTimeInterface|string $value
*
* @return string
*/
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());
2023-02-20 10:54:29 +03:00
return 'Invalid Date-Time';
}
2023-02-20 10:54:29 +03:00
}
}
if (!function_exists('format_timezone')) {
/**
* Format a timezone string based on the current app settings.
*
* @param string $value
*
* @return string
*/
function format_timezone(string $value): string
{
/** @var EA_Controller $CI */
$CI = &get_instance();
$CI->load->library('timezones');
return $CI->timezones->get_timezone_name($value);
}
}