setTimezone($timezone); } catch (\Throwable $exception) { return self::formatDateTimeSeparator($part); } return $dateTime->format(self::buildPhpDateTimeFormat($dateFormat, $timeFormat)); } /** * Build a combined PHP date-time format string from JS-style tokens and a time format key. * * @param string $dateFormat JS-style date format (e.g. DD.MM.YYYY) * @param string $timeFormat Time format key: 'ampm', '24h', 'ampm-s', or '24h-s' * @return string PHP date() compatible format */ private static function buildPhpDateTimeFormat(string $dateFormat, string $timeFormat): string { $phpDateFormat = str_replace( ['YYYY', 'MM', 'DD'], ['Y', 'm', 'd'], $dateFormat ?: 'Y-m-d' ); $timeFormatMap = [ 'ampm' => 'g:i A', 'ampm-s' => 'g:i:s A', '24h' => 'H:i', '24h-s' => 'H:i:s', ]; $phpTimeFormat = $timeFormatMap[$timeFormat] ?? 'H:i'; return $phpDateFormat . ' ' . $phpTimeFormat; } /** * Format Unix timestamp back to datetime string. * Includes seconds when the timestamp has a non-zero seconds component. * * @param int $timestamp Unix timestamp * @return string Formatted datetime string */ private static function timestampToDateTime(int $timestamp): string { $timezone = DateTimeHelper::getWpTimezone(); $dateTime = new DateTimeImmutable('@' . $timestamp); $dateTime = $dateTime->setTimezone($timezone); $format = ((int) $dateTime->format('s') > 0) ? 'Y-m-d H:i:s' : 'Y-m-d H:i'; return $dateTime->format($format); } /** * Normalize display spacing around the datetime separator. * * @param string $value Raw datetime string * @return string Datetime string with spaced separator */ private static function formatDateTimeSeparator(string $value): string { return (string) preg_replace('/\s*\|\s*/', ' ', $value); } }