AlertProvider.php
1 year ago
ApiClient.php
1 year ago
DateTimeHelper.php
1 year ago
DateTimes.php
1 year ago
DateTimesImmutable.php
1 year ago
Debugger.php
1 year ago
DisplayHelper.php
1 year ago
EmailHelper.php
1 year ago
PostTypeHelper.php
1 year ago
Random.php
1 year ago
SecurityValidator.php
1 year ago
ShortcodeSubstitutor.php
1 year ago
TemplateProvider.php
1 year ago
functions.php
1 year ago
DateTimeHelper.php
57 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace FapiMember\Utils; |
| 4 | |
| 5 | use DateTime; |
| 6 | use DateTimeImmutable; |
| 7 | use FapiMember\Model\Enums\Format; |
| 8 | |
| 9 | class DateTimeHelper |
| 10 | { |
| 11 | |
| 12 | public static function getNow(): DateTimeImmutable |
| 13 | { |
| 14 | return new DateTimeImmutable('now', wp_timezone()); |
| 15 | } |
| 16 | |
| 17 | public static function getNowTimestamp(): int |
| 18 | { |
| 19 | $now = self::getNow(); |
| 20 | |
| 21 | return strtotime($now->format(Format::DATE_TIME)); |
| 22 | } |
| 23 | |
| 24 | public static function createOrNull(string|null $dateTimeString, string $format): DateTimeImmutable|null |
| 25 | { |
| 26 | if ($dateTimeString === null) { |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | $dateTime = DateTimeImmutable::createFromFormat( |
| 31 | $format, |
| 32 | $dateTimeString, |
| 33 | wp_timezone(), |
| 34 | ); |
| 35 | |
| 36 | return $dateTime !== false ? $dateTime : null; |
| 37 | } |
| 38 | |
| 39 | public static function getNextFullHour(): DateTime |
| 40 | { |
| 41 | $dateTime = new DateTime(); |
| 42 | $dateTime->setTimestamp(time()); |
| 43 | $dateTime->modify('+1 hour'); |
| 44 | $dateTime->setTime((int)$dateTime->format('H'), 0); |
| 45 | |
| 46 | return $dateTime; |
| 47 | } |
| 48 | |
| 49 | public static function getDaysDifference(DateTimeImmutable $from, DateTimeImmutable $to): int |
| 50 | { |
| 51 | $interval = $from->diff($to); |
| 52 | |
| 53 | return (int) $interval->format('%r%a'); |
| 54 | } |
| 55 | |
| 56 | } |
| 57 |