PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Common / Helpers / DateTimeHelper / DateHelper.php

DateHelper.php in The Innovative Form Builder – IvyForms 1.5.1, at backend/src/Common/Helpers/DateTimeHelper/DateHelper.php

97 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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