DateManager.php
3 weeks ago
EventManager.php
3 weeks ago
LogManager.php
3 weeks ago
OptionManager.php
3 weeks ago
PolicyManager.php
3 weeks ago
DateManager.php
213 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Date and time operations using native PHP DateTime. |
| 5 | * Centralizes date/time operations with WordPress timezone awareness. |
| 6 | * |
| 7 | * @package Framework |
| 8 | * @subpackage Managers |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | namespace Kirki\Framework\Managers; |
| 12 | |
| 13 | \defined('ABSPATH') || exit; |
| 14 | use DateTime; |
| 15 | use DateTimeInterface; |
| 16 | use DateTimeZone; |
| 17 | use Exception; |
| 18 | use Kirki\Framework\Constants\DateTimeFormats; |
| 19 | class DateManager |
| 20 | { |
| 21 | /** |
| 22 | * Default SQL datetime format string. |
| 23 | * |
| 24 | * @var string |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | */ |
| 28 | public const BASE_FORMAT = DateTimeFormats::DB_DATETIME; |
| 29 | /** |
| 30 | * Get the current date and time. |
| 31 | * |
| 32 | * @param \DateTimeZone|string|int|null $timezone Optional timezone. |
| 33 | * |
| 34 | * @return \DateTime |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | public function now($timezone = null) |
| 39 | { |
| 40 | return new DateTime('now', $this->resolve_timezone($timezone)); |
| 41 | } |
| 42 | /** |
| 43 | * Parse a value into a DateTime instance. |
| 44 | * |
| 45 | * @param \DateTimeInterface|string|int|float|null $time The value to parse. |
| 46 | * @param \DateTimeZone|string|int|null $timezone Optional timezone. |
| 47 | * |
| 48 | * @return \DateTime |
| 49 | * |
| 50 | * @since 1.0.0 |
| 51 | */ |
| 52 | public function parse($time, $timezone = null) |
| 53 | { |
| 54 | $resolved_timezone = $this->resolve_timezone($timezone); |
| 55 | if ($time instanceof DateTimeInterface) { |
| 56 | $date = $this->to_datetime($time); |
| 57 | return $date->setTimezone($resolved_timezone); |
| 58 | } |
| 59 | if (\is_int($time) || \is_float($time)) { |
| 60 | return $this->create_from_timestamp($time, $resolved_timezone); |
| 61 | } |
| 62 | return new DateTime((string) $time, $resolved_timezone); |
| 63 | } |
| 64 | /** |
| 65 | * Create a DateTime instance from an existing date object. |
| 66 | * |
| 67 | * @param \DateTimeInterface $date The source date. |
| 68 | * |
| 69 | * @return \DateTime |
| 70 | * |
| 71 | * @since 1.0.0 |
| 72 | */ |
| 73 | public function instance(DateTimeInterface $date) |
| 74 | { |
| 75 | return $this->to_datetime($date); |
| 76 | } |
| 77 | /** |
| 78 | * Create a DateTime instance from a format string. |
| 79 | * |
| 80 | * @param string $format The expected format. |
| 81 | * @param string $time The date string. |
| 82 | * @param \DateTimeZone|string|int|null $timezone Optional timezone. |
| 83 | * |
| 84 | * @return \DateTime|null |
| 85 | * |
| 86 | * @since 1.0.0 |
| 87 | */ |
| 88 | public function create_from_format($format, $time, $timezone = null) |
| 89 | { |
| 90 | $date = DateTime::createFromFormat($format, $time, $this->resolve_timezone($timezone)); |
| 91 | if ($date === \false) { |
| 92 | return null; |
| 93 | } |
| 94 | return $date; |
| 95 | } |
| 96 | /** |
| 97 | * Create a DateTime instance from a Unix timestamp. |
| 98 | * |
| 99 | * @param string|int|float $timestamp The Unix timestamp. |
| 100 | * @param \DateTimeZone|string|int|null $timezone Optional timezone. |
| 101 | * |
| 102 | * @return \DateTime |
| 103 | * |
| 104 | * @since 1.0.0 |
| 105 | */ |
| 106 | public function create_from_timestamp($timestamp, $timezone = null) |
| 107 | { |
| 108 | $date = DateTime::createFromFormat('U', (string) $timestamp); |
| 109 | return $date->setTimezone($this->resolve_timezone($timezone)); |
| 110 | } |
| 111 | /** |
| 112 | * Check if the value is a valid date. |
| 113 | * |
| 114 | * @param mixed $value The value to validate. |
| 115 | * |
| 116 | * @return bool |
| 117 | * |
| 118 | * @since 1.0.0 |
| 119 | */ |
| 120 | public function is_valid_date($value) |
| 121 | { |
| 122 | if ($value instanceof DateTimeInterface) { |
| 123 | return \true; |
| 124 | } |
| 125 | if (!\is_string($value) && !\is_numeric($value)) { |
| 126 | return \false; |
| 127 | } |
| 128 | try { |
| 129 | $this->parse($value); |
| 130 | return \true; |
| 131 | } catch (Exception $exception) { |
| 132 | return \false; |
| 133 | } |
| 134 | } |
| 135 | /** |
| 136 | * Set the time of a date to the start of the day. |
| 137 | * |
| 138 | * @param \DateTimeInterface $date The source date. |
| 139 | * |
| 140 | * @return \DateTime |
| 141 | * |
| 142 | * @since 1.0.0 |
| 143 | */ |
| 144 | public function start_of_day(DateTimeInterface $date) |
| 145 | { |
| 146 | $start_of_day = $this->to_datetime($date); |
| 147 | $start_of_day->setTime(0, 0, 0); |
| 148 | return $start_of_day; |
| 149 | } |
| 150 | /** |
| 151 | * Determine whether a date is after another date. |
| 152 | * |
| 153 | * @param \DateTimeInterface $date The date to compare. |
| 154 | * @param \DateTimeInterface $other The reference date. |
| 155 | * |
| 156 | * @return bool |
| 157 | * |
| 158 | * @since 1.0.0 |
| 159 | */ |
| 160 | public function is_after(DateTimeInterface $date, DateTimeInterface $other) |
| 161 | { |
| 162 | return $date > $other; |
| 163 | } |
| 164 | /** |
| 165 | * Convert a date to a SQL safe datetime string. |
| 166 | * |
| 167 | * @param \DateTimeInterface $date The date to format. |
| 168 | * |
| 169 | * @return string |
| 170 | * |
| 171 | * @since 1.0.0 |
| 172 | */ |
| 173 | public function to_sql_datetime_string(DateTimeInterface $date) |
| 174 | { |
| 175 | return $date->format(static::BASE_FORMAT); |
| 176 | } |
| 177 | /** |
| 178 | * Resolve a timezone value to a DateTimeZone instance. |
| 179 | * |
| 180 | * @param \DateTimeZone|string|int|null $timezone Optional timezone. |
| 181 | * |
| 182 | * @return \DateTimeZone |
| 183 | * |
| 184 | * @since 1.0.0 |
| 185 | */ |
| 186 | protected function resolve_timezone($timezone = null) |
| 187 | { |
| 188 | if ($timezone instanceof DateTimeZone) { |
| 189 | return $timezone; |
| 190 | } |
| 191 | if (\is_string($timezone) && $timezone !== '') { |
| 192 | return new DateTimeZone($timezone); |
| 193 | } |
| 194 | return new DateTimeZone(\date_default_timezone_get()); |
| 195 | } |
| 196 | /** |
| 197 | * Convert a DateTimeInterface instance to a mutable DateTime. |
| 198 | * |
| 199 | * @param \DateTimeInterface $date The source date. |
| 200 | * |
| 201 | * @return \DateTime |
| 202 | * |
| 203 | * @since 1.0.0 |
| 204 | */ |
| 205 | protected function to_datetime(DateTimeInterface $date) |
| 206 | { |
| 207 | if ($date instanceof DateTime) { |
| 208 | return clone $date; |
| 209 | } |
| 210 | return new DateTime($date->format('Y-m-d H:i:s.u'), $date->getTimezone()); |
| 211 | } |
| 212 | } |
| 213 |