| @@ -92,18 +92,10 @@ | ||
| 92 | 92 | * |
| 93 | 93 | * @return array<string, string> |
| 94 | 94 | */ |
| 95 | 95 | public static function from_timestamp( int $timestamp, ?DateTimeZone $timezone = null, ?string $locale = null ): array { |
| 96 | - $timezone = $timezone ? $timezone : self::get_default_timezone(); | |
| 97 | - $locale = $locale ? $locale : self::get_default_locale(); | |
| 98 | - $date = ( new DateTimeImmutable( '@' . $timestamp ) )->setTimezone( $timezone ); | |
| 96 | + list( $date, $timezone, $locale, $hour_token, $fallback_time ) = self::resolve_context( $timestamp, $timezone, $locale ); | |
| 99 | 97 | |
| 100 | - // The WordPress time_format setting controls the clock convention | |
| 101 | - // (12 vs 24-hour); the locale keeps controlling localized date text. | |
| 102 | - $time_format = self::get_time_format_option(); | |
| 103 | - $hour_token = self::get_icu_hour_token( $time_format ); | |
| 104 | - $fallback_time = '' !== $time_format ? $time_format : 'g:i A'; | |
| 105 | - | |
| 106 | 98 | return array( |
| 107 | 99 | 'datetime' => self::format_style( $date, $timezone, $locale, self::INTL_MEDIUM, self::INTL_SHORT, 'M j, Y ' . $fallback_time, $hour_token ), |
| 108 | 100 | 'date' => self::format_style( $date, $timezone, $locale, self::INTL_MEDIUM, self::INTL_NONE, 'M j, Y' ), |
| 109 | 101 | 'time' => self::format_style( $date, $timezone, $locale, self::INTL_NONE, self::INTL_SHORT, $fallback_time, $hour_token ), |
| @@ -126,8 +118,45 @@ | ||
| 126 | 118 | ); |
| 127 | 119 | } |
| 128 | 120 | |
| 129 | 121 | /** |
| 122 | + * Format the time of day on its own. | |
| 123 | + * | |
| 124 | + * Returns exactly the value `from_timestamp()` puts in its `time` key, so | |
| 125 | + * every clock rendered on a receipt — order timestamps, opening hours — | |
| 126 | + * comes from one convention source. | |
| 127 | + * | |
| 128 | + * @param int $timestamp Unix timestamp. | |
| 129 | + * @param DateTimeZone|null $timezone Optional timezone override. | |
| 130 | + * @param string|null $locale Optional locale override. | |
| 131 | + * | |
| 132 | + * @return string | |
| 133 | + */ | |
| 134 | + public static function time( int $timestamp, ?DateTimeZone $timezone = null, ?string $locale = null ): string { | |
| 135 | + list( $date, $timezone, $locale, $hour_token, $fallback_time ) = self::resolve_context( $timestamp, $timezone, $locale ); | |
| 136 | + | |
| 137 | + return self::format_style( $date, $timezone, $locale, self::INTL_NONE, self::INTL_SHORT, $fallback_time, $hour_token ); | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 141 | + * Format the short weekday name on its own. | |
| 142 | + * | |
| 143 | + * Returns exactly the value `from_timestamp()` puts in its `weekday_short` | |
| 144 | + * key, so day labels match wherever a receipt names a weekday. | |
| 145 | + * | |
| 146 | + * @param int $timestamp Unix timestamp. | |
| 147 | + * @param DateTimeZone|null $timezone Optional timezone override. | |
| 148 | + * @param string|null $locale Optional locale override. | |
| 149 | + * | |
| 150 | + * @return string | |
| 151 | + */ | |
| 152 | + public static function weekday_short( int $timestamp, ?DateTimeZone $timezone = null, ?string $locale = null ): string { | |
| 153 | + list( $date, $timezone, $locale ) = self::resolve_context( $timestamp, $timezone, $locale ); | |
| 154 | + | |
| 155 | + return self::format_pattern( $date, $timezone, $locale, 'EEE', 'D' ); | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 130 | 159 | * Build an empty date structure. |
| 131 | 160 | * |
| 132 | 161 | * @return array<string, string> |
| 133 | 162 | */ |
| @@ -135,8 +164,36 @@ | ||
| 135 | 164 | return array_fill_keys( self::DATE_FIELDS, '' ); |
| 136 | 165 | } |
| 137 | 166 | |
| 138 | 167 | /** |
| 168 | + * Resolve the shared inputs every formatting method needs. | |
| 169 | + * | |
| 170 | + * The WordPress time_format setting controls the clock convention (12 vs | |
| 171 | + * 24-hour); the locale keeps controlling localized date text. | |
| 172 | + * | |
| 173 | + * @param int $timestamp Unix timestamp. | |
| 174 | + * @param DateTimeZone|null $timezone Optional timezone override. | |
| 175 | + * @param string|null $locale Optional locale override. | |
| 176 | + * | |
| 177 | + * @return array{0: DateTimeImmutable, 1: DateTimeZone, 2: string, 3: string|null, 4: string} | |
| 178 | + */ | |
| 179 | + private static function resolve_context( int $timestamp, ?DateTimeZone $timezone, ?string $locale ): array { | |
| 180 | + $timezone = $timezone ? $timezone : self::get_default_timezone(); | |
| 181 | + $locale = $locale ? $locale : self::get_default_locale(); | |
| 182 | + $date = ( new DateTimeImmutable( '@' . $timestamp ) )->setTimezone( $timezone ); | |
| 183 | + | |
| 184 | + $time_format = self::get_time_format_option(); | |
| 185 | + | |
| 186 | + return array( | |
| 187 | + $date, | |
| 188 | + $timezone, | |
| 189 | + $locale, | |
| 190 | + self::get_icu_hour_token( $time_format ), | |
| 191 | + '' !== $time_format ? $time_format : 'g:i A', | |
| 192 | + ); | |
| 193 | + } | |
| 194 | + | |
| 195 | + /** | |
| 139 | 196 | * Format using Intl styles with a sane fallback. |
| 140 | 197 | * |
| 141 | 198 | * @param DateTimeInterface $date Date to format. |
| 142 | 199 | * @param DateTimeZone $timezone Date timezone. |
| @@ -434,8 +491,9 @@ | ||
| 434 | 491 | |
| 435 | 492 | if ( $use_24_hour ) { |
| 436 | 493 | // Strip removed day-period markers with their surrounding spacing |
| 437 | 494 | // (including NBSP/NNBSP used by newer CLDR data). |
| 495 | + $result = (string) preg_replace( "/[\s\x{00A0}\x{202F}]*\x01+[\s\x{00A0}\x{202F}]*(?!')(?=[\p{Pe}\p{Pf}\p{Po}])/u", '', $result ); | |
| 438 | 496 | $result = (string) preg_replace( '/[\s\x{00A0}\x{202F}]*\x01+[\s\x{00A0}\x{202F}]*/u', ' ', $result ); |
| 439 | 497 | $result = (string) preg_replace( '/[\s\x{00A0}\x{202F}]{2,}/u', ' ', $result ); |
| 440 | 498 | |
| 441 | 499 | return trim( $result ); |