| 1 | <?php |
||
| 2 | |||
| 3 | namespace Common\Tool; |
||
| 4 | |||
| 5 | use DateTime; |
||
| 6 | |||
| 7 | class DateTool |
||
| 8 | { |
||
| 9 | |||
| 10 | public static function convertToDate(\DateTimeInterface $d): DateTime |
||
| 11 | { |
||
| 12 | return DateTime::createFromFormat('Ymd', $d->format('Ymd'))->setTime(0, 0, 0); |
||
| 13 | } |
||
| 14 | |||
| 15 | public static function greaterDate(\DateTimeInterface $d1, \DateTimeInterface $d2): bool |
||
| 16 | { |
||
| 17 | return self::convertToDate($d1) > self::convertToDate($d2); |
||
| 18 | } |
||
| 19 | |||
| 20 | public static function smallerDate(\DateTimeInterface $d1, \DateTimeInterface $d2): bool |
||
| 21 | { |
||
| 22 | return self::convertToDate($d1) < self::convertToDate($d2); |
||
| 23 | } |
||
| 24 | |||
| 25 | public static function sameDate(\DateTimeInterface $d1, \DateTimeInterface $d2): bool |
||
| 26 | { |
||
| 27 | return self::convertToDate($d1) == self::convertToDate($d2); |
||
| 28 | } |
||
| 29 | |||
| 30 | public static function greaterOrSameDate(\DateTimeInterface $d1, \DateTimeInterface $d2): bool |
||
| 31 | { |
||
| 32 | return self::convertToDate($d1) >= self::convertToDate($d2); |
||
| 33 | } |
||
| 34 | |||
| 35 | public static function smallerOrSameDate(?\DateTimeInterface $d1, ?\DateTimeInterface $d2): bool |
||
| 36 | { |
||
| 37 | if(null === $d1) { |
||
| 38 | return true; |
||
| 39 | } |
||
| 40 | |||
| 41 | if(null === $d2) { |
||
| 42 | return false; |
||
| 43 | } |
||
| 44 | |||
| 45 | return self::convertToDate($d1) <= self::convertToDate($d2); |
||
| 46 | } |
||
| 47 | |||
| 48 | public static function inDateRange(\DateTimeInterface $date, \DateTimeInterface $periodFrom, \DateTimeInterface $periodTo): bool |
||
| 49 | { |
||
| 50 | return self::greaterOrSameDate($date, $periodFrom) && self::smallerOrSameDate($date, $periodTo); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * http://stackoverflow.com/questions/14202687/how-can-i-find-overlapping-dateperiods-date-ranges-in-php |
||
| 55 | * @return int |
||
| 56 | */ |
||
| 57 | public static function datesOverlap($start_one, $end_one, $start_two, $end_two): int |
||
| 58 | { |
||
| 59 | if ($start_one <= $end_two && $end_one >= $start_two) { //If the dates overlap |
||
| 60 | return min($end_one, $end_two)->diff(max($start_two, $start_one))->days + 1; //return how many days overlap |
||
| 61 | } |
||
| 62 | |||
| 63 | return 0; //Return 0 if there is no overlap |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Add months _without overlapping_ |
||
| 68 | * 31/01 + 1 month = 28/02 |
||
| 69 | * |
||
| 70 | * @param \DateTimeInterface $date |
||
| 71 | * @param int $months |
||
| 72 | * @return \DateTimeInterface |
||
| 73 | */ |
||
| 74 | public static function addMonths(\DateTime $date, int $months): \DateTime |
||
| 75 | { |
||
| 76 | $startDay = $date->format('j'); |
||
| 77 | |||
| 78 | $date->modify("+{$months} month"); |
||
| 79 | |||
| 80 | $endDay = $date->format('j'); |
||
| 81 | |||
| 82 | if ($startDay != $endDay) { |
||
| 83 | $date->modify('last day of last month'); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $date; |
||
| 87 | } |
||
| 88 | |||
| 89 | public static function addDays(\DateTime $date, $days): \DateTime |
||
| 90 | { |
||
| 91 | $date->modify(sprintf('+%d day%s', $days, $days > 1 ? 's' : '')); |
||
| 92 | |||
| 93 | return $date; |
||
| 94 | } |
||
| 95 | |||
| 96 | public static function removeDays(\DateTime $date, $days): \DateTime |
||
| 97 | { |
||
| 98 | $date->modify(sprintf('-%d day%s', $days, $days > 1 ? 's' : '')); |
||
| 99 | |||
| 100 | return $date; |
||
| 101 | } |
||
| 102 | |||
| 103 | public static function currentDate(): \DateTime |
||
| 104 | { |
||
| 105 | return new \DateTime('today'); |
||
| 106 | } |
||
| 107 | |||
| 108 | public static function getDaysBetweenDates(\DateTimeInterface $d1, \DateTimeInterface $d2) |
||
| 109 | { |
||
| 110 | $d1 = self::convertToDate($d1); |
||
| 111 | $d2 = self::convertToDate($d2); |
||
| 112 | |||
| 113 | return $d1->diff($d2)->days; |
||
| 114 | } |
||
| 115 | |||
| 116 | public static function toText(\DateTimeInterface $date, ?string $country = null): string |
||
|
0 ignored issues
–
show
|
|||
| 117 | { |
||
| 118 | return $date->format('F jS Y'); |
||
| 119 | } |
||
| 120 | |||
| 121 | public static function valid($data): bool |
||
| 122 | { |
||
| 123 | return $data instanceof \DateTimeInterface; |
||
| 124 | } |
||
| 125 | |||
| 126 | public static function yesterdayDate(): \DateTime |
||
| 127 | { |
||
| 128 | $yesterday = (new \DateTime)->modify('yesterday'); |
||
| 129 | |||
| 130 | return self::convertToDate($yesterday); |
||
| 131 | } |
||
| 132 | } |
||
| 133 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.