ArrayUtil.php
1 year ago
CallbackUtil.php
4 months ago
DiscountsUtil.php
2 years ago
FeaturesUtil.php
4 months ago
I18nUtil.php
3 years ago
LoggingUtil.php
1 year ago
MetaDataUtil.php
4 weeks ago
NumberUtil.php
10 months ago
OrderUtil.php
6 months ago
PluginUtil.php
7 months ago
RestApiUtil.php
6 months ago
ShippingUtil.php
1 year ago
StringUtil.php
1 year ago
TimeUtil.php
2 years ago
TimeUtil.php
49 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Utilities; |
| 4 | |
| 5 | use \DateTime; |
| 6 | use \DateTimeZone; |
| 7 | use \Exception; |
| 8 | |
| 9 | /** |
| 10 | * Class with date and time utilities. |
| 11 | */ |
| 12 | class TimeUtil { |
| 13 | |
| 14 | /** |
| 15 | * Instance of a DateTimeZone object representing UTC. |
| 16 | * |
| 17 | * @var DateTimeZone |
| 18 | */ |
| 19 | private static DateTimeZone $utc_date_time_zone; |
| 20 | |
| 21 | /** |
| 22 | * Class constructor. |
| 23 | */ |
| 24 | public function __construct() { |
| 25 | self::$utc_date_time_zone = new DateTimeZone( 'UTC' ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Get the instance of the DateTimeZone object representing UTC. |
| 30 | * |
| 31 | * @return DateTimeZone DateTimeZone object representing UTC. |
| 32 | */ |
| 33 | public static function get_utc_date_time_zone(): DateTimeZone { |
| 34 | return self::$utc_date_time_zone; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Check if a string represents a valid date in a given format. |
| 39 | * |
| 40 | * @param string $date The date string to check. |
| 41 | * @param string $format The format to verify the date string against. |
| 42 | * @return bool True if $date represents a valid date/time according to $format, false otherwise. |
| 43 | */ |
| 44 | public static function is_valid_date( string $date, string $format = 'Y-m-d H:i:s' ): bool { |
| 45 | $d = DateTime::createFromFormat( $format, $date ); |
| 46 | return $d && $d->format( $format ) === $date; |
| 47 | } |
| 48 | } |
| 49 |