'', 'ymd-dashes' => 'Y-m-d', 'mdy-dashes' => 'm-d-Y', 'dmy-dashes' => 'd-m-Y', 'dmy-short-year' => 'd-m-y', 'mdy-slashes' => 'm/d/Y', 'dmy-slashes' => 'd/m/Y', 'month-day-year' => 'F j, Y', 'day-month-year' => 'j F Y', ); private const TIME_FORMATS = array( 'wordpress' => '', 'time-24' => 'H:i', 'time-12' => 'g:i a', ); /** * Returns the configured business timezone. */ public static function business_timezone(): DateTimeZone { $settings = get_option( 'abientot_settings', array() ); $timezone = is_array( $settings ) && ! empty( $settings['business_timezone'] ) ? (string) $settings['business_timezone'] : wp_timezone_string(); try { return new DateTimeZone( $timezone ); } catch ( Exception $exception ) { return wp_timezone(); } } /** * Returns a UTC timezone instance. */ public static function utc_timezone(): DateTimeZone { return new DateTimeZone( 'UTC' ); } /** * Allowed date display formats. * * @return array */ public static function date_format_choices(): array { return self::DATE_FORMATS; } /** * Allowed time display formats. * * @return array */ public static function time_format_choices(): array { return self::TIME_FORMATS; } /** * Whether an Abientot language override differs from the site language. * WordPress's date and time format options then belong to another language. */ private static function language_override_active(): bool { $locale = Settings::configured_locale(); return '' !== $locale && get_locale() !== $locale; } /** * WordPress's date format, or the plugin language's own default under an override. */ public static function wordpress_date_format(): string { if ( self::language_override_active() ) { /* translators: Default date format for this language, as PHP date() characters (https://www.php.net/manual/datetime.format.php). Escape literal letters with a backslash. */ return _x( 'F j, Y', 'default date format', 'abientot' ); } return (string) get_option( 'date_format', 'Y-m-d' ); } /** * WordPress's time format, or the plugin language's own default under an override. */ public static function wordpress_time_format(): string { if ( self::language_override_active() ) { /* translators: Default time format for this language, as PHP date() characters (https://www.php.net/manual/datetime.format.php). Escape literal letters with a backslash. */ return _x( 'g:i a', 'default time format', 'abientot' ); } return (string) get_option( 'time_format', 'H:i' ); } /** * Weekday with full date, e.g. headings. */ public static function full_date_format(): string { /* translators: Weekday and full date format for headings, as PHP date() characters (https://www.php.net/manual/datetime.format.php). Escape literal letters with a backslash. */ return _x( 'l, j F Y', 'full date format', 'abientot' ); } /** * Month and year, e.g. calendar month titles. */ public static function month_year_format(): string { /* translators: Month and year format for calendar titles, as PHP date() characters (https://www.php.net/manual/datetime.format.php). Escape literal letters with a backslash. */ return _x( 'F Y', 'month and year format', 'abientot' ); } /** * Returns the configured human-readable date format. */ public static function date_display_format(): string { $settings = get_option( 'abientot_settings', array() ); $value = is_array( $settings ) && isset( $settings['date_display_format'] ) ? (string) $settings['date_display_format'] : 'wordpress'; if ( ! array_key_exists( $value, self::DATE_FORMATS ) || 'wordpress' === $value ) { return self::wordpress_date_format(); } return self::DATE_FORMATS[ $value ]; } /** * Returns the configured human-readable time format. */ public static function time_display_format(): string { $settings = get_option( 'abientot_settings', array() ); $value = is_array( $settings ) && isset( $settings['time_display_format'] ) ? (string) $settings['time_display_format'] : 'wordpress'; if ( ! array_key_exists( $value, self::TIME_FORMATS ) || 'wordpress' === $value ) { return self::wordpress_time_format(); } return self::TIME_FORMATS[ $value ]; } /** * Returns the configured human-readable datetime format. */ public static function datetime_display_format(): string { return trim( self::date_display_format() . ' ' . self::time_display_format() ); } /** * Formats a UTC SQL datetime for display in a given local timezone. */ public static function format_utc_datetime( string $utc_datetime, ?string $timezone = null, string $format = '' ): string { $format = '' !== $format ? $format : self::datetime_display_format(); try { $date = new DateTimeImmutable( $utc_datetime, self::utc_timezone() ); return Locale::date( $format, $date->getTimestamp(), self::timezone_from_string( $timezone ) ); } catch ( Exception $exception ) { return get_date_from_gmt( $utc_datetime, $format ); } } /** * Formats a local SQL datetime for display. */ public static function format_local_datetime( string $local_datetime, ?string $timezone = null, string $format = '' ): string { $format = '' !== $format ? $format : self::datetime_display_format(); try { $timezone_object = self::timezone_from_string( $timezone ); $date = new DateTimeImmutable( $local_datetime, $timezone_object ); return Locale::date( $format, $date->getTimestamp(), $timezone_object ); } catch ( Exception $exception ) { return $local_datetime; } } /** * Formats a local SQL datetime as a display time. */ public static function format_local_time( string $local_datetime, ?string $timezone = null ): string { return self::format_local_datetime( $local_datetime, $timezone, self::time_display_format() ); } private static function timezone_from_string( ?string $timezone ): DateTimeZone { try { return new DateTimeZone( $timezone ?: self::business_timezone()->getName() ); } catch ( Exception $exception ) { return self::business_timezone(); } } /** * Returns the current UTC SQL datetime. */ public static function now_utc_sql(): string { return gmdate( 'Y-m-d H:i:s' ); } /** * Converts a local SQL datetime into UTC SQL datetime. */ public static function local_to_utc_sql( string $local_datetime, ?DateTimeZone $timezone = null ): string { $timezone = $timezone ?: self::business_timezone(); $date = new DateTimeImmutable( $local_datetime, $timezone ); return $date->setTimezone( self::utc_timezone() )->format( 'Y-m-d H:i:s' ); } /** * Converts a UTC SQL datetime into local SQL datetime. */ public static function utc_to_local_sql( string $utc_datetime, ?DateTimeZone $timezone = null ): string { $timezone = $timezone ?: self::business_timezone(); $date = new DateTimeImmutable( $utc_datetime, self::utc_timezone() ); return $date->setTimezone( $timezone )->format( 'Y-m-d H:i:s' ); } }