TemporalFacade.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Support\Facades\DateTime; |
| 4 | |
| 5 | use DateTime; |
| 6 | use DateTimeImmutable; |
| 7 | use DateTimeInterface; |
| 8 | use DateTimeZone; |
| 9 | use Exception; |
| 10 | use Give\Log\Log; |
| 11 | |
| 12 | /** |
| 13 | * @since 2.19.6 |
| 14 | */ |
| 15 | class TemporalFacade |
| 16 | { |
| 17 | /** |
| 18 | * @since 2.20.0 minor clean up and add types to signature |
| 19 | * @since 2.19.6 |
| 20 | */ |
| 21 | public function toDateTime(string $date): DateTimeInterface |
| 22 | { |
| 23 | return DateTime::createFromFormat('Y-m-d H:i:s', $date, wp_timezone()); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @since 2.20.0 simplify and add types to signature |
| 28 | * @since 2.19.6 |
| 29 | */ |
| 30 | public function getCurrentDateTime(): DateTimeInterface |
| 31 | { |
| 32 | return new DateTime('now', wp_timezone()); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @since 2.19.6 |
| 37 | * |
| 38 | * @param DateTimeInterface $dateTime |
| 39 | * |
| 40 | * @return string |
| 41 | */ |
| 42 | public function getFormattedDateTime(DateTimeInterface $dateTime) |
| 43 | { |
| 44 | return $dateTime->format('Y-m-d H:i:s'); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @since 2.19.6 |
| 49 | * |
| 50 | * @return string |
| 51 | */ |
| 52 | public function getCurrentFormattedDateForDatabase() |
| 53 | { |
| 54 | return current_datetime()->format('Y-m-d H:i:s'); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Immutably returns a new DateTime instance with the microseconds set to 0. |
| 59 | * |
| 60 | * @since 4.0.0 Extracted new immutableOrClone method. |
| 61 | * @since 2.20.0 |
| 62 | */ |
| 63 | public function withoutMicroseconds(DateTimeInterface $dateTime) |
| 64 | { |
| 65 | return $this |
| 66 | ->immutableOrClone($dateTime) |
| 67 | ->setTime( |
| 68 | $dateTime->format('H'), |
| 69 | $dateTime->format('i'), |
| 70 | $dateTime->format('s') |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Immutably returns a new DateTime instance with the time set to the start of the day. |
| 76 | * |
| 77 | * @since 4.0.0 |
| 78 | */ |
| 79 | public function withStartOfDay(DateTimeInterface $dateTime): DateTimeInterface |
| 80 | { |
| 81 | return $this |
| 82 | ->immutableOrClone($dateTime) |
| 83 | ->setTime(0, 0, 0, 0); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Immutably returns a new DateTime instance with the time set to the end of the day. |
| 88 | * |
| 89 | * @since 4.0.0 |
| 90 | */ |
| 91 | public function withEndOfDay(DateTimeInterface $dateTime): DateTimeInterface |
| 92 | { |
| 93 | return $this |
| 94 | ->immutableOrClone($dateTime) |
| 95 | ->setTime(23, 59, 59, 999999); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @since 4.0.0 |
| 100 | */ |
| 101 | public function immutableOrClone(DateTimeInterface $dateTime): DateTimeInterface |
| 102 | { |
| 103 | return $dateTime instanceof DateTimeImmutable |
| 104 | ? $dateTime |
| 105 | : clone $dateTime; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @since 3.20.0 |
| 110 | */ |
| 111 | public function getDateTimestamp(string $date, string $timezone = ''): int |
| 112 | { |
| 113 | try { |
| 114 | $timezone = empty($timezone) ? wp_timezone_string() : $timezone; |
| 115 | $timezone = new DateTimeZone($timezone); |
| 116 | $date = new DateTime($date, $timezone); |
| 117 | |
| 118 | return $date->getTimestamp(); |
| 119 | } catch (Exception $e) { |
| 120 | Log::error( |
| 121 | 'Failed to parse date string into a timestamp', |
| 122 | [ |
| 123 | 'input_date' => $date, |
| 124 | 'input_timezone' => $timezone, |
| 125 | 'error_code' => $e->getCode(), |
| 126 | 'error_message' => $e->getMessage(), |
| 127 | ] |
| 128 | ); |
| 129 | return 0; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get the formatted date time using the site specific time zone and format settings |
| 135 | * |
| 136 | * @since 4.13.0 |
| 137 | */ |
| 138 | public function getFormattedDateTimeUsingTimeZoneAndFormatSettings(DateTimeInterface $dateTime): string |
| 139 | { |
| 140 | $dateFormat = get_option('date_format'); |
| 141 | $timeFormat = get_option('time_format'); |
| 142 | |
| 143 | $format = sprintf('%s \a\t %s', $dateFormat, $timeFormat); |
| 144 | |
| 145 | return wp_date($format, $dateTime->getTimestamp(), wp_timezone()); |
| 146 | } |
| 147 | } |
| 148 |