| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Common\Helpers\DateTimeHelper; |
| 4 |
|
| 5 |
use DateTimeImmutable; |
| 6 |
|
| 7 |
/** |
| 8 |
* Helper utilities for date operations. |
| 9 |
*/ |
| 10 |
class DateHelper |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Normalize date values to Unix timestamp format. |
| 14 |
* |
| 15 |
* @param int|string|null $value Field value |
| 16 |
* @return string Normalized value as string |
| 17 |
*/ |
| 18 |
public static function normalizeDateValue($value): string |
| 19 |
{ |
| 20 |
if ($value === null || $value === '') { |
| 21 |
return ''; |
| 22 |
} |
| 23 |
|
| 24 |
return (string) (self::dateToTimestamp($value) ?? $value); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Parse date string to Unix timestamp. |
| 29 |
* |
| 30 |
* @param string $dateString Date string to parse |
| 31 |
* @return int|null Unix timestamp or null if invalid |
| 32 |
*/ |
| 33 |
public static function parseDateStringToTimestamp(string $dateString): ?int |
| 34 |
{ |
| 35 |
try { |
| 36 |
$timezone = DateTimeHelper::getWpTimezone(); |
| 37 |
$dateTime = new DateTimeImmutable($dateString, $timezone); |
| 38 |
|
| 39 |
// Set time to midnight (00:00:00) for date-only values |
| 40 |
$dateTime = $dateTime->setTime(0, 0, 0); |
| 41 |
|
| 42 |
return $dateTime->getTimestamp(); |
| 43 |
} catch (\Throwable $exception) { |
| 44 |
return null; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Format Unix timestamp back to date string (YYYY-MM-DD). |
| 50 |
* |
| 51 |
* @param int $timestamp Unix timestamp |
| 52 |
* @return string Formatted date string |
| 53 |
*/ |
| 54 |
public static function timestampToDate(int $timestamp): string |
| 55 |
{ |
| 56 |
$timezone = DateTimeHelper::getWpTimezone(); |
| 57 |
$dateTime = new DateTimeImmutable('@' . $timestamp); |
| 58 |
$dateTime = $dateTime->setTimezone($timezone); |
| 59 |
return $dateTime->format('Y-m-d'); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Format Unix timestamp to a custom date format. |
| 64 |
* |
| 65 |
* @param int $timestamp Unix timestamp |
| 66 |
* @param string $format PHP date format string |
| 67 |
* @return string Formatted date string |
| 68 |
*/ |
| 69 |
public static function timestampToDateFormat(int $timestamp, string $format): string |
| 70 |
{ |
| 71 |
$timezone = DateTimeHelper::getWpTimezone(); |
| 72 |
$dateTime = new DateTimeImmutable('@' . $timestamp); |
| 73 |
$dateTime = $dateTime->setTimezone($timezone); |
| 74 |
return $dateTime->format($format); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Convert date string (YYYY-MM-DD or various formats) to Unix timestamp. |
| 79 |
* |
| 80 |
* @param mixed $value Date value (YYYY-MM-DD, or numeric timestamp) |
| 81 |
* @return int|null Unix timestamp or null if invalid |
| 82 |
*/ |
| 83 |
private static function dateToTimestamp($value): ?int |
| 84 |
{ |
| 85 |
if ($value === null || $value === '') { |
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
if (is_numeric($value)) { |
| 90 |
return (int) $value; |
| 91 |
} |
| 92 |
|
| 93 |
$dateString = trim((string) $value); |
| 94 |
return self::parseDateStringToTimestamp($dateString); |
| 95 |
} |
| 96 |
} |
| 97 |
|