PHP 8.4+ Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
CodeHydrater/src/classes/time_zones.php

274 lines
8.9 KiB

<?php
declare(strict_types=1);
/**
* @author Robert Strutts
* @copyright Copyright (c) 2022, Robert Strutts.
* @license MIT
*/
namespace CodeHydrater;
final class time_zones {
/**
* MySQL time conversion!
* @param type $field time
* @param type $time_zone_name, leave blank for users time zone!
* @return type
*/
public static function db_convert_tz($field, $time_zone_name = '') {
$session_time_zone = (isset($_SESSION['time_zone'])) ? $_SESSION['time_zone'] : 'America/Detroit';
$tz = (!empty($time_zone_name) ) ? $time_zone_name : $session_time_zone;
$to_tz = self::current_time_for_time_zone($tz);
return "CONVERT_TZ({$field}, '+00:00', '{$to_tz}')";
}
/**
* Purpose: To convert time zone to offset time
* @param type $time_zone_name
* @return offset
*/
public static function current_time_for_time_zone($time_zone_name) {
$time = new \DateTime('now', new \DateTimeZone($time_zone_name));
return $time->format('P');
}
/**
* Purpose: To make a temporary valid timestamp for a database
*/
public static function expires_in_hours(int $hours = 1) {
$hours = ($hours > 0 && $hours < 10) ? $hours : 1;
$expires = new DateTime('NOW', new \DateTimeZone('UTC'));
$expires->add(new DateInterval("PT0{$hours}H"));
return $expires->format('Y-m-d H:i:s');
}
private static function mdy($userTime, $format) {
switch (bootstrap\common::string_to_lowercase($format)) {
case 'actions':
return $userTime->format('m-d-Y / h:iA');
case 'report':
return $userTime->format('l jS \of F\, Y\, h:i a');
case 'fancy':
return $userTime->format('l jS \of F Y h:i:s A');
case 'logging':
case 'log':
return $userTime->format('g:i A \o\n l jS F Y');
case 'legal_date':
return $userTime->format('n/j/Y');
case 'date':
return $userTime->format('m/d/Y');
case 'date-time':
return $userTime->format('m/d/Y h:i:s A');
case 'full':
return $userTime->format('m-d-Y h:i:s A');
case 'normal':
return $userTime->format('m/d/Y h:i A');
case 'default':
return $userTime->format('m-d-Y h:i A');
default:
return $userTime->format($format);
}
}
private static function dmy($userTime, $format) {
switch (bootstrap\common::string_to_lowercase($format)) {
case 'actions':
return $userTime->format('d-m-Y / h:iA');
case 'report':
return $userTime->format('l jS \of F\, Y\, h:i a');
case 'fancy':
return $userTime->format('l jS \of F Y h:i:s A');
case 'logging':
case 'log':
return $userTime->format('g:i A \o\n l jS F Y');
case 'legal_date':
return $userTime->format('j/n/Y');
case 'date':
return $userTime->format('d/m/Y');
case 'date-time':
return $userTime->format('d/m/Y h:i:s A');
case 'full':
return $userTime->format('d-m-Y h:i:s A');
case 'normal':
return $userTime->format('d/m/Y h:i A');
case 'default':
return $userTime->format('d-m-Y h:i A');
default:
return $userTime->format($format);
}
}
private static function ymd($userTime, $format) {
switch (bootstrap\common::string_to_lowercase($format)) {
case 'actions':
return $userTime->format('Y-m-d / h:iA');
case 'report':
return $userTime->format('l jS \of F\, Y\, h:i a');
case 'fancy':
return $userTime->format('l jS \of F Y h:i:s A');
case 'logging':
case 'log':
return $userTime->format('g:i A \o\n l jS F Y');
case 'legal_date':
return $userTime->format('Y/n/j');
case 'date':
return $userTime->format('Y/m/d');
case 'date-time':
return $userTime->format('Y/m/d h:i:s A');
case 'full':
return $userTime->format('Y-m-d h:i:s A');
case 'normal':
return $userTime->format('Y/m/d h:i A');
case 'default':
return $userTime->format('Y-m-d h:i A');
default:
return $userTime->format($format);
}
}
public static function dt_format($userTime, $format, $country) {
switch (bootstrap\common::string_to_lowercase($format)) {
case 'object':
return $userTime;
case 'unix':
return $userTime->format('U');
case 'day':
return $userTime->format('l');
case 'time':
return $userTime->format('h:i A');
case 'military':
return $userTime->format('H:i:s');
case 'atom':
return $userTime->format(DateTime::ATOM);
case 'cookie':
return $userTime->format(DateTime::COOKIE);
case 'iso8601':
case 'iso':
case '8601':
return $userTime->format(DateTime::ISO8601);
case 'rfc822':
return $userTime->format(DateTime::RFC822);
case 'rfc850':
return $userTime->format(DateTime::RFC850);
case 'rfc1036':
return $userTime->format(DateTime::RFC1036);
case 'rfc1123':
return $userTime->format(DateTime::RFC1123);
case 'rfc2822':
return $userTime->format(DateTime::RFC2822);
case 'rfc3339':
return $userTime->format(DateTime::RFC3339);
case 'rss':
return $userTime->format(DateTime::RSS);
case 'w3c':
return $userTime->format(DateTime::W3C);
case 'standard':
case 'computer':
case 'database':
return $userTime->format('Y-m-d H:i:s');
}
switch (bootstrap\common::string_to_lowercase($country)) {
case 'china':
return self::ymd($userTime, $format);
case 'int':
case 'other':
return self::dmy($userTime, $format);
default:
return self::mdy($userTime, $format);
}
}
/**
* Purpose: To convert a database timestamp into the users own Time Zone.
*/
public static function convert_time_zone($options) {
$format = (isset($options['format'])) ? $options['format'] : 'normal';
$session_time_zone = (isset($_SESSION['time_zone'])) ? $_SESSION['time_zone'] : 'America/Detroit';
$tz = (isset($options['time_zone']) && !empty($options['time_zone'])) ? $options['time_zone'] : $session_time_zone;
$offset = (isset($options['offset'])) ? $options['offset'] : '';
$db_time = (isset($options['time'])) ? $options['time'] : '';
if ($db_time === '0000-00-00 00:00:00') {
return false;
}
$new_time = (empty($db_time)) ? self::get_offset($offset) : self::get_offset_by($offset, $db_time);
// Convert date("U"); unix timestamps to proper format for DateTime function...
if (substr_count($new_time, ':') == 0) {
$the_time = (empty($new_time) || $new_time == 'now' || $new_time == 'current') ? gmdate("Y-m-d H:i:s") : gmdate("Y-m-d H:i:s", $new_time);
}
$userTime = new \DateTime($the_time, new \DateTimeZone('UTC'));
// Set the users time_zone to their zone
if ($tz !== 'UTC') {
$userTime->setTimezone(new \DateTimeZone($tz));
}
$country = (isset($options['country'])) ? $options['country'] : 'usa';
return self::dt_format($userTime, $format, $country);
}
public static function human_date($input_date) {
if (empty($input_date)) {
return '';
}
$today = self::convert_time_zone(array('format' => 'm/d/Y'));
$date = strtotime($input_date);
if (date('m/d/Y', $date) == $today) {
return date('g:i', $date) . "<span style=\"font-size: 0.65em; vertical-align: text-top; margin-left: 1px; text-transform: lowercase;\">" . date('A', $date) . "</span>";
} elseif (date('Y', $date) == date('Y')) {
return "<span style=\"font-weight: 500;\">" . date('M', $date) . " " . date('j', $date) . "<span style=\"font-size: 0.65em; vertical-align: text-top; margin-left: 1px;\">" . date('S', $date) . "</span></span>";
} else {
return date('m/d/y', $date);
}
}
private static function is_valid_offset($offset) {
if (substr_count($offset, 'second') > 0) {
return true;
} elseif (substr_count($offset, 'minute') > 0) {
return true;
} elseif (substr_count($offset, 'hour') > 0) {
return true;
} elseif (substr_count($offset, 'day') > 0) {
return true;
} elseif (substr_count($offset, 'week') > 0) {
return true;
} elseif (substr_count($offset, 'month') > 0) {
return true;
} elseif (substr_count($offset, 'year') > 0) {
return true;
} elseif (substr_count($offset, 'next') > 0) {
return true;
} elseif (substr_count($offset, 'last') > 0) {
return true;
} else {
return false;
}
}
private static function get_offset($offset) {
return (self::is_valid_offset($offset)) ? strtotime($offset) : $offset;
}
private static function get_offset_by($offset, $db_time) {
// strtotime requires a int timestamp
if (substr_count($db_time, ':') > 0) {
$UTC = new \DateTime($db_time, new \DateTimeZone('UTC'));
$db_time = $UTC->format('U');
}
return (self::is_valid_offset($offset)) ? strtotime($offset, $db_time) : $db_time;
}
}