DateTimeService.php
287 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © TMS-Plugins. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Domain\Services\DateTime; |
| 9 | |
| 10 | /** |
| 11 | * Class DateTimeService |
| 12 | * |
| 13 | * @package AmeliaBooking\Domain\Services\DateTime |
| 14 | */ |
| 15 | class DateTimeService |
| 16 | { |
| 17 | /** @var \DateTimeZone */ |
| 18 | private static $timeZone; |
| 19 | |
| 20 | /** |
| 21 | * @param array $settings |
| 22 | */ |
| 23 | public static function setTimeZone($settings) |
| 24 | { |
| 25 | if (!self::$timeZone) { |
| 26 | if ($settings['timeZoneString']) { |
| 27 | self::$timeZone = new \DateTimeZone($settings['timeZoneString']); |
| 28 | } elseif ($settings['gmtOffset']) { |
| 29 | $hours = (int)$settings['gmtOffset']; |
| 30 | $minutes = ($settings['gmtOffset'] - floor($settings['gmtOffset'])) * 60; |
| 31 | |
| 32 | self::$timeZone = new \DateTimeZone(sprintf('%+03d:%02d', $hours, $minutes)); |
| 33 | } else { |
| 34 | self::$timeZone = new \DateTimeZone('UTC'); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return \DateTimeZone |
| 41 | */ |
| 42 | public static function getTimeZone() |
| 43 | { |
| 44 | return self::$timeZone; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Return now date and time object by timezone settings |
| 49 | * |
| 50 | * @return \DateTime |
| 51 | */ |
| 52 | public static function getNowDateTimeObject() |
| 53 | { |
| 54 | return new \DateTime('now', self::getTimeZone()); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Return now date and time string by timezone settings |
| 59 | * |
| 60 | * @return string |
| 61 | */ |
| 62 | public static function getNowDateTime() |
| 63 | { |
| 64 | return self::getNowDateTimeObject()->format('Y-m-d H:i:s'); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param String $dateTimeString |
| 69 | * |
| 70 | * @return \DateTime |
| 71 | */ |
| 72 | public static function getCustomDateTimeObject($dateTimeString) |
| 73 | { |
| 74 | return new \DateTime($dateTimeString ? $dateTimeString : 'now', self::getTimeZone()); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Return custom date and time string by timezone settings |
| 79 | * |
| 80 | * @param String $dateTimeString |
| 81 | * |
| 82 | * @return string |
| 83 | */ |
| 84 | public static function getCustomDateTime($dateTimeString) |
| 85 | { |
| 86 | return self::getCustomDateTimeObject($dateTimeString)->format('Y-m-d H:i:s'); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param String $dateTimeString |
| 91 | * @param int $clientUtcOffset |
| 92 | * |
| 93 | * @return \DateTime |
| 94 | */ |
| 95 | public static function getClientUtcCustomDateTimeObject($dateTimeString, $clientUtcOffset) |
| 96 | { |
| 97 | $clientDateTime = new \DateTime($dateTimeString, new \DateTimeZone('UTC')); |
| 98 | |
| 99 | if ($clientUtcOffset > 0) { |
| 100 | $clientDateTime->modify("+{$clientUtcOffset} minutes"); |
| 101 | } elseif ($clientUtcOffset < 0) { |
| 102 | $clientDateTime->modify("{$clientUtcOffset} minutes"); |
| 103 | } |
| 104 | |
| 105 | return $clientDateTime; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Return custom date and time string by utc offset |
| 110 | * |
| 111 | * @param String $dateTimeString |
| 112 | * @param int $clientUtcOffset |
| 113 | * |
| 114 | * @return string |
| 115 | */ |
| 116 | public static function getClientUtcCustomDateTime($dateTimeString, $clientUtcOffset) |
| 117 | { |
| 118 | return self::getClientUtcCustomDateTimeObject($dateTimeString, $clientUtcOffset)->format('Y-m-d H:i:s'); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Return now date and time object in UTC |
| 123 | * |
| 124 | * @return \DateTime |
| 125 | */ |
| 126 | public static function getNowDateTimeObjectInUtc() |
| 127 | { |
| 128 | return self::getNowDateTimeObject()->setTimezone(new \DateTimeZone('UTC')); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Return now date and time string in UTC |
| 133 | * |
| 134 | * @return string |
| 135 | */ |
| 136 | public static function getNowDateTimeInUtc() |
| 137 | { |
| 138 | return self::getNowDateTimeObjectInUtc()->format('Y-m-d H:i:s'); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Return custom date and time object in UTC |
| 143 | * |
| 144 | * @param $dateTimeString |
| 145 | * |
| 146 | * @return \DateTime |
| 147 | */ |
| 148 | public static function getCustomDateTimeObjectInUtc($dateTimeString) |
| 149 | { |
| 150 | return self::getCustomDateTimeObject($dateTimeString)->setTimezone(new \DateTimeZone('UTC')); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Return custom date and time object in UTC |
| 155 | * |
| 156 | * @param $dateTimeString |
| 157 | * @param $timezone |
| 158 | * |
| 159 | * @return \DateTime |
| 160 | */ |
| 161 | public static function getCustomDateTimeObjectInTimeZone($dateTimeString, $timezone) |
| 162 | { |
| 163 | return self::getCustomDateTimeObject($dateTimeString)->setTimezone(new \DateTimeZone($timezone)); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Return custom date and time object in UTC |
| 168 | * |
| 169 | * @param $dateTimeString |
| 170 | * @param $timezone |
| 171 | * |
| 172 | * @return \DateTime |
| 173 | */ |
| 174 | public static function getDateTimeObjectInTimeZone($dateTimeString, $timezone) |
| 175 | { |
| 176 | return (new \DateTime($dateTimeString, new \DateTimeZone($timezone))); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Return custom date and time string in UTC |
| 181 | * |
| 182 | * @param $dateTimeString |
| 183 | * |
| 184 | * @return string |
| 185 | */ |
| 186 | public static function getCustomDateTimeInUtc($dateTimeString) |
| 187 | { |
| 188 | return self::getCustomDateTimeObjectInUtc($dateTimeString)->format('Y-m-d H:i:s'); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Return custom date and time object from UTC |
| 193 | * |
| 194 | * @param $dateTimeString |
| 195 | * |
| 196 | * @return \DateTime |
| 197 | */ |
| 198 | public static function getCustomDateTimeObjectFromUtc($dateTimeString) |
| 199 | { |
| 200 | return (new \DateTime($dateTimeString, new \DateTimeZone('UTC')))->setTimezone(self::getTimeZone()); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Return custom date and time string from UTC |
| 205 | * |
| 206 | * @param $dateTimeString |
| 207 | * |
| 208 | * @return string |
| 209 | */ |
| 210 | public static function getCustomDateTimeFromUtc($dateTimeString) |
| 211 | { |
| 212 | return self::getCustomDateTimeObjectFromUtc($dateTimeString)->format('Y-m-d H:i:s'); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Return custom date and time RFC3339 from UTC |
| 217 | * |
| 218 | * @param string $dateTimeString |
| 219 | * |
| 220 | * @return string |
| 221 | */ |
| 222 | public static function getCustomDateTimeRFC3339($dateTimeString) |
| 223 | { |
| 224 | return self::getCustomDateTimeObjectInUtc($dateTimeString)->format(DATE_RFC3339); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Return now date string by timezone settings |
| 229 | * |
| 230 | * @return string |
| 231 | */ |
| 232 | public static function getNowDate() |
| 233 | { |
| 234 | return self::getNowDateTimeObject()->format('Y-m-d'); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Return now time string by timezone settings |
| 239 | * |
| 240 | * @return string |
| 241 | */ |
| 242 | public static function getNowTime() |
| 243 | { |
| 244 | return self::getNowDateTimeObject()->format('H:i:s'); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Return current Unix timestamp |
| 249 | * |
| 250 | * @return false|int |
| 251 | */ |
| 252 | public static function getNowTimestamp() |
| 253 | { |
| 254 | return strtotime(self::getNowTime()); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Return Day Index for passed date string in 'YYYY-MM-DD' format. |
| 259 | * Monday index is 1, Sunday index is 7. |
| 260 | * |
| 261 | * @param $dateString |
| 262 | * |
| 263 | * @return int |
| 264 | */ |
| 265 | public static function getDayIndex($dateString) |
| 266 | { |
| 267 | return self::getCustomDateTimeObject($dateString)->format('N'); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * @param array $dateTimes |
| 272 | * |
| 273 | * @return array |
| 274 | */ |
| 275 | public static function getSortedDateTimeStrings($dateTimes) |
| 276 | { |
| 277 | usort( |
| 278 | $dateTimes, |
| 279 | function ($a, $b) { |
| 280 | return strtotime($a) - strtotime($b); |
| 281 | } |
| 282 | ); |
| 283 | |
| 284 | return $dateTimes; |
| 285 | } |
| 286 | } |
| 287 |