AlertProvider.php
1 year ago
ApiClient.php
1 day ago
DateTimeHelper.php
1 year ago
DateTimes.php
2 years ago
DateTimesImmutable.php
2 years ago
Debugger.php
1 year ago
DisplayHelper.php
1 year ago
EmailHelper.php
1 year ago
PostTypeHelper.php
2 years ago
Random.php
4 years ago
SecurityValidator.php
2 years ago
ShortcodeSubstitutor.php
2 months ago
TemplateProvider.php
2 years ago
functions.php
3 years ago
DateTimeHelper.php
109 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace FapiMember\Utils; |
| 4 | |
| 5 | use DateInterval; |
| 6 | use DatePeriod; |
| 7 | use DateTime; |
| 8 | use DateTimeImmutable; |
| 9 | use FapiMember\Model\Enums\Format; |
| 10 | |
| 11 | class DateTimeHelper |
| 12 | { |
| 13 | |
| 14 | public static function getNow(): DateTimeImmutable |
| 15 | { |
| 16 | $nowLocal = new DateTimeImmutable('now', wp_timezone()); |
| 17 | return new DateTimeImmutable($nowLocal->format(Format::DATE_TIME)); |
| 18 | } |
| 19 | |
| 20 | public static function getNowTimestamp(): int |
| 21 | { |
| 22 | $now = self::getNow(); |
| 23 | |
| 24 | return strtotime($now->format(Format::DATE_TIME)); |
| 25 | } |
| 26 | |
| 27 | public static function createOrNull(string|null $dateTimeString, string $format): DateTimeImmutable|null |
| 28 | { |
| 29 | if ($dateTimeString === null) { |
| 30 | return null; |
| 31 | } |
| 32 | |
| 33 | $dateTime = DateTimeImmutable::createFromFormat( |
| 34 | $format, |
| 35 | $dateTimeString, |
| 36 | wp_timezone(), |
| 37 | ); |
| 38 | |
| 39 | return $dateTime !== false ? $dateTime : null; |
| 40 | } |
| 41 | |
| 42 | public static function getNextFullHour(): DateTime |
| 43 | { |
| 44 | $dateTime = new DateTime(); |
| 45 | $dateTime->setTimestamp(time()); |
| 46 | $dateTime->modify('+1 hour'); |
| 47 | $dateTime->setTime((int)$dateTime->format('H'), 0); |
| 48 | |
| 49 | return $dateTime; |
| 50 | } |
| 51 | |
| 52 | public static function getDaysDifference(DateTimeImmutable $from, DateTimeImmutable $to): int |
| 53 | { |
| 54 | $interval = $from->diff($to); |
| 55 | |
| 56 | return (int) $interval->format('%r%a'); |
| 57 | } |
| 58 | |
| 59 | public static function calculateGraphPeriods( |
| 60 | DateTimeImmutable $dateFrom, |
| 61 | DateTimeImmutable $dateTo, |
| 62 | $maxDataPoints = 61, |
| 63 | ): array |
| 64 | { |
| 65 | if ($dateTo > DateTimeHelper::getNow()) { |
| 66 | $dateTo = DateTimeHelper::getNow(); |
| 67 | } |
| 68 | |
| 69 | $dateFrom = $dateFrom->setTime(0, 0, 0); |
| 70 | $dateTo = $dateTo->setTime(23, 59, 59); |
| 71 | |
| 72 | $periods = []; |
| 73 | $totalDays = $dateTo->diff($dateFrom)->days; |
| 74 | |
| 75 | if ($totalDays <= $maxDataPoints) { |
| 76 | $interval = new DateInterval('P1D'); |
| 77 | } elseif ($totalDays <= $maxDataPoints * 3) { |
| 78 | $interval = new DateInterval('P3D'); |
| 79 | } elseif ($totalDays <= $maxDataPoints * 7) { |
| 80 | $interval = new DateInterval('P1W'); |
| 81 | } elseif ($totalDays <= $maxDataPoints * 30) { |
| 82 | $interval = new DateInterval('P1M'); |
| 83 | } elseif ($totalDays <= $maxDataPoints * 365) { |
| 84 | $interval = new DateInterval('P1Y'); |
| 85 | } else { |
| 86 | $yearsPerPeriod = ceil($totalDays / 365 / $maxDataPoints); |
| 87 | $interval = new DateInterval("P{$yearsPerPeriod}Y"); |
| 88 | } |
| 89 | |
| 90 | $period = new DatePeriod($dateFrom, $interval, $dateTo); |
| 91 | |
| 92 | foreach ($period as $startDate) { |
| 93 | $endDate = $startDate->add($interval)->sub(new DateInterval('P1D')); |
| 94 | |
| 95 | if ($endDate > $dateTo) { |
| 96 | $endDate = $dateTo; |
| 97 | } |
| 98 | |
| 99 | $periods[] = [ |
| 100 | 'date_from' => $startDate->setTime(0, 0, 0), |
| 101 | 'date_to' => $endDate->setTime(23, 59, 59), |
| 102 | ]; |
| 103 | } |
| 104 | |
| 105 | return $periods; |
| 106 | } |
| 107 | |
| 108 | } |
| 109 |