| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Common\Helpers\DateTimeHelper; |
| 4 |
|
| 5 |
use DateTimeImmutable; |
| 6 |
use RuntimeException; |
| 7 |
use IvyForms\Services\Translations\BackendStrings; |
| 8 |
|
| 9 |
/** |
| 10 |
* Helper utilities for time operations. |
| 11 |
*/ |
| 12 |
class TimeHelper |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Normalize time values to Unix timestamp format. |
| 16 |
* |
| 17 |
* @param int|string|null $value Field value |
| 18 |
* @return string Normalized value as string |
| 19 |
*/ |
| 20 |
public static function normalizeTimeValue($value): string |
| 21 |
{ |
| 22 |
if ($value === null || $value === '') { |
| 23 |
return ''; |
| 24 |
} |
| 25 |
|
| 26 |
return (string) (self::timeToTimestamp($value) ?? $value); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Format Unix timestamp back to time string (HH:MM). |
| 31 |
* |
| 32 |
* @param int $timestamp Unix timestamp |
| 33 |
* @return string Formatted time string without seconds |
| 34 |
*/ |
| 35 |
public static function timestampToTime(int $timestamp): string |
| 36 |
{ |
| 37 |
$timezone = DateTimeHelper::getWpTimezone(); |
| 38 |
$dateTime = new DateTimeImmutable('@' . $timestamp); |
| 39 |
$dateTime = $dateTime->setTimezone($timezone); |
| 40 |
return $dateTime->format('H:i'); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Format Unix timestamp back to time string with seconds (HH:MM:SS). |
| 45 |
* |
| 46 |
* @param int $timestamp Unix timestamp |
| 47 |
* @return string Formatted time string with seconds |
| 48 |
*/ |
| 49 |
public static function timestampToTimeWithSeconds(int $timestamp): string |
| 50 |
{ |
| 51 |
$timezone = DateTimeHelper::getWpTimezone(); |
| 52 |
$dateTime = new DateTimeImmutable('@' . $timestamp); |
| 53 |
$dateTime = $dateTime->setTimezone($timezone); |
| 54 |
return $dateTime->format('H:i:s'); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Convert time string (HH:MM or HH:MM:SS) to Unix timestamp using current date in WP timezone. |
| 59 |
* |
| 60 |
* @param mixed $value Time value (HH:MM, HH:MM:SS, or numeric timestamp) |
| 61 |
* @return int|null Unix timestamp or null if invalid |
| 62 |
*/ |
| 63 |
private static function timeToTimestamp($value): ?int |
| 64 |
{ |
| 65 |
if ($value === null || $value === '') { |
| 66 |
return null; |
| 67 |
} |
| 68 |
|
| 69 |
if (is_numeric($value)) { |
| 70 |
return (int) $value; |
| 71 |
} |
| 72 |
|
| 73 |
$timeString = trim((string) $value); |
| 74 |
return self::parseTimeStringToTimestamp($timeString); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Parse time string (HH:MM or HH:MM:SS) to Unix timestamp. |
| 79 |
* |
| 80 |
* @param string $timeString Time string to parse |
| 81 |
* @return int|null Unix timestamp or null if invalid |
| 82 |
*/ |
| 83 |
private static function parseTimeStringToTimestamp(string $timeString): ?int |
| 84 |
{ |
| 85 |
if (!preg_match('/^(\d{1,2}):(\d{2})(?::(\d{2}))?$/', $timeString, $matches)) { |
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
$hours = (int) $matches[1]; |
| 90 |
$minutes = (int) $matches[2]; |
| 91 |
$seconds = isset($matches[3]) ? (int) $matches[3] : 0; |
| 92 |
|
| 93 |
if (!self::isValidTimeComponents($hours, $minutes, $seconds)) { |
| 94 |
return null; |
| 95 |
} |
| 96 |
|
| 97 |
return self::createTimestampFromTime($hours, $minutes, $seconds); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Validate time components. |
| 102 |
* |
| 103 |
* @param int $hours Hours (0-23) |
| 104 |
* @param int $minutes Minutes (0-59) |
| 105 |
* @param int $seconds Seconds (0-59) |
| 106 |
* @return bool True if valid |
| 107 |
*/ |
| 108 |
private static function isValidTimeComponents(int $hours, int $minutes, int $seconds): bool |
| 109 |
{ |
| 110 |
return $hours <= 23 && $minutes <= 59 && $seconds <= 59; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Create Unix timestamp from time components using today's date. |
| 115 |
* |
| 116 |
* @param int $hours Hours |
| 117 |
* @param int $minutes Minutes |
| 118 |
* @param int $seconds Seconds |
| 119 |
* @return int Unix timestamp |
| 120 |
* @throws RuntimeException If unable to create timestamp |
| 121 |
*/ |
| 122 |
private static function createTimestampFromTime(int $hours, int $minutes, int $seconds): int |
| 123 |
{ |
| 124 |
try { |
| 125 |
$timezone = DateTimeHelper::getWpTimezone(); |
| 126 |
$today = new DateTimeImmutable('today', $timezone); |
| 127 |
$dateString = $today->format('Y-m-d'); |
| 128 |
$timeString = sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); |
| 129 |
$dateTime = new DateTimeImmutable("$dateString $timeString", $timezone); |
| 130 |
return $dateTime->getTimestamp(); |
| 131 |
} catch (\Throwable $exception) { |
| 132 |
throw new RuntimeException( |
| 133 |
BackendStrings::getExceptionStrings()['time_conversion_failed'] . ': ' . $exception->getMessage() |
| 134 |
); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|