| @@ -1,66 +1,94 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace Booktics\Services; | |
| 4 | - | |
| 5 | -use DateTime; | |
| 6 | -use DateTimeZone; | |
| 7 | - | |
| 8 | -/** | |
| 9 | - * Handles timezone conversions and validations for date/time operations | |
| 10 | - */ | |
| 11 | -class Time_Zone_Handler { | |
| 12 | - /** | |
| 13 | - * Converts a local time to UTC | |
| 14 | - * | |
| 15 | - * @param string $time Time in format H:i:s | |
| 16 | - * @param string $date Date in format Y-m-d | |
| 17 | - * @param string $timezone Source timezone identifier | |
| 18 | - * | |
| 19 | - * @return array Array containing converted 'time' and 'date' | |
| 20 | - */ | |
| 21 | - public static function convert_to_utc( string $time, string $date, string $timezone ) { | |
| 22 | - $datetime = new DateTime( "$date $time", new DateTimeZone( $timezone ) ); | |
| 23 | - $datetime->setTimezone( new DateTimeZone( 'UTC' ) ); | |
| 24 | - | |
| 25 | - return array( | |
| 26 | - 'time' => $datetime->format( 'H:i:s' ), | |
| 27 | - 'date' => $datetime->format( 'Y-m-d' ), | |
| 28 | - ); | |
| 29 | - } | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * Converts a UTC time to local timezone | |
| 33 | - * | |
| 34 | - * @param string $time Time in format H:i:s | |
| 35 | - * @param string $date Date in format Y-m-d | |
| 36 | - * @param string $timezone Target timezone identifier | |
| 37 | - * | |
| 38 | - * @return array Array containing converted 'time' and 'date' | |
| 39 | - */ | |
| 40 | - /** | |
| 41 | - * @return array | |
| 42 | - */ | |
| 43 | - public static function convertFromUTC( string $time, string $date, string $timezone ) { | |
| 44 | - $datetime = new DateTime( "$date $time", new DateTimeZone( 'UTC' ) ); | |
| 45 | - $datetime->setTimezone( new DateTimeZone( $timezone ) ); | |
| 46 | - | |
| 47 | - return array( | |
| 48 | - 'time' => $datetime->format( 'H:i:s' ), | |
| 49 | - 'date' => $datetime->format( 'Y-m-d' ), | |
| 50 | - ); | |
| 51 | - } | |
| 52 | - | |
| 53 | - /** | |
| 54 | - * Validates if the provided timezone identifier is valid | |
| 55 | - * | |
| 56 | - * @param string $timezone Timezone identifier to validate | |
| 57 | - * | |
| 58 | - * @return bool True if timezone is valid, false otherwise | |
| 59 | - */ | |
| 60 | - /** | |
| 61 | - * @return bool | |
| 62 | - */ | |
| 63 | - public static function validate_time_zone( string $timezone ) { | |
| 64 | - return in_array( $timezone, DateTimeZone::listIdentifiers() ); | |
| 65 | - } | |
| 66 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Booktics\Services; | |
| 4 | + | |
| 5 | +use DateTime; | |
| 6 | +use DateTimeZone; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * Handles timezone conversions and validations for date/time operations | |
| 10 | + */ | |
| 11 | +class Time_Zone_Handler { | |
| 12 | + /** | |
| 13 | + * Converts a local time to UTC | |
| 14 | + * | |
| 15 | + * @param string $time Time in format H:i:s | |
| 16 | + * @param string $date Date in format Y-m-d | |
| 17 | + * @param string $timezone Source timezone identifier | |
| 18 | + * | |
| 19 | + * @return array Array containing converted 'time' and 'date' | |
| 20 | + */ | |
| 21 | + public static function convert_to_utc( string $time, string $date, string $timezone ) { | |
| 22 | + // If date already contains timezone information, use it directly | |
| 23 | + if (strpos($date, 'T') !== false) { | |
| 24 | + $date = substr($date, 0, strpos($date, 'T')); // Extract just the date part (before the 'T') | |
| 25 | + } | |
| 26 | + | |
| 27 | + $datetime = new DateTime( "$date $time", new DateTimeZone( $timezone ) ); | |
| 28 | + $datetime->setTimezone( new DateTimeZone( 'UTC' ) ); | |
| 29 | + | |
| 30 | + return array( | |
| 31 | + 'time' => $datetime->format( 'H:i:s' ), | |
| 32 | + 'date' => $datetime->format( 'Y-m-d' ), | |
| 33 | + ); | |
| 34 | + } | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Converts a UTC time to local timezone | |
| 38 | + * | |
| 39 | + * @param string $time Time in format H:i:s | |
| 40 | + * @param string $date Date in format Y-m-d | |
| 41 | + * @param string $timezone Target timezone identifier | |
| 42 | + * | |
| 43 | + * @return array Array containing converted 'time' and 'date' | |
| 44 | + */ | |
| 45 | + /** | |
| 46 | + * @return array | |
| 47 | + */ | |
| 48 | + public static function convertFromUTC( string $time, string $date, string $timezone ) { | |
| 49 | + $datetime = new DateTime( "$date $time", new DateTimeZone( 'UTC' ) ); | |
| 50 | + $datetime->setTimezone( new DateTimeZone( $timezone ) ); | |
| 51 | + | |
| 52 | + return array( | |
| 53 | + 'time' => $datetime->format( 'H:i:s' ), | |
| 54 | + 'date' => $datetime->format( 'Y-m-d' ), | |
| 55 | + ); | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Validates if the provided timezone identifier is valid | |
| 60 | + * | |
| 61 | + * @param string $timezone Timezone identifier to validate | |
| 62 | + * | |
| 63 | + * @return bool True if timezone is valid, false otherwise | |
| 64 | + */ | |
| 65 | + /** | |
| 66 | + * @return bool | |
| 67 | + */ | |
| 68 | + public static function validate_time_zone( string $timezone ) { | |
| 69 | + return in_array( $timezone, DateTimeZone::listIdentifiers() ); | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Handles timezone string to DateTimeZone object conversion | |
| 74 | + * In case the timezone string is invalid, returns default timezone | |
| 75 | + * If timezone is already a DateTimeZone object, returns it as it is | |
| 76 | + * | |
| 77 | + * @param string|DateTimeZone $timezone Timezone string | |
| 78 | + * | |
| 79 | + * @return DateTimeZone | |
| 80 | + */ | |
| 81 | + public static function get_timezone_from_string( $timezone ) { | |
| 82 | + // If timezone is already a DateTimeZone object, return as it is | |
| 83 | + if ( $timezone instanceof DateTimeZone ) { | |
| 84 | + return $timezone; | |
| 85 | + } | |
| 86 | + // Incase of invalid timezone string, return default timezone | |
| 87 | + if ( ! is_string( $timezone ) ) { | |
| 88 | + return wp_timezone(); | |
| 89 | + } | |
| 90 | + | |
| 91 | + // Validate and return a DateTimeZone object | |
| 92 | + return self::validate_time_zone( $timezone ) ? new DateTimeZone($timezone) : wp_timezone(); | |
| 93 | + } | |
| 94 | +} | |