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