Created
April 28, 2019 17:20
-
-
Save UchePhilz/37d5d778b1dfc4112453af6e5460fcf4 to your computer and use it in GitHub Desktop.
Get the first and last date of period (year, quarter,'month, week)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static function firstDayOf($period, \DateTime $date = null) { | |
| $period = strtolower($period); | |
| $validPeriods = array('year', 'quarter', 'month', 'week'); | |
| if (!in_array($period, $validPeriods)) | |
| throw new \InvalidArgumentException('Period must be one of: ' . implode(', ', $validPeriods)); | |
| $newDate = ($date === null) ? new \DateTime() : clone $date; | |
| switch ($period) { | |
| case 'year': | |
| $newDate->modify('first day of january ' . $newDate->format('Y')); | |
| break; | |
| case 'quarter': | |
| $month = $newDate->format('n'); | |
| if ($month < 4) { | |
| $newDate->modify('first day of january ' . $newDate->format('Y')); | |
| } elseif ($month > 3 && $month < 7) { | |
| $newDate->modify('first day of april ' . $newDate->format('Y')); | |
| } elseif ($month > 6 && $month < 10) { | |
| $newDate->modify('first day of july ' . $newDate->format('Y')); | |
| } elseif ($month > 9) { | |
| $newDate->modify('first day of october ' . $newDate->format('Y')); | |
| } | |
| break; | |
| case 'month': | |
| $newDate->modify('first day of this month'); | |
| break; | |
| case 'week': | |
| $newDate->modify(($newDate->format('w') === '0') ? 'monday last week' : 'monday this week'); | |
| break; | |
| } | |
| return $newDate->format('Y-m-d'); | |
| } | |
| public static function lastDayOf($period, \DateTime $date = null) { | |
| $period = strtolower($period); | |
| $validPeriods = array('year', 'quarter', 'month', 'week'); | |
| if (!in_array($period, $validPeriods)) | |
| throw new \InvalidArgumentException('Period must be one of: ' . implode(', ', $validPeriods)); | |
| $newDate = ($date === null) ? new \DateTime() : clone $date; | |
| switch ($period) { | |
| case 'year': | |
| $newDate->modify('last day of december ' . $newDate->format('Y')); | |
| break; | |
| case 'quarter': | |
| $month = $newDate->format('n'); | |
| if ($month < 4) { | |
| $newDate->modify('last day of march ' . $newDate->format('Y')); | |
| } elseif ($month > 3 && $month < 7) { | |
| $newDate->modify('last day of june ' . $newDate->format('Y')); | |
| } elseif ($month > 6 && $month < 10) { | |
| $newDate->modify('last day of september ' . $newDate->format('Y')); | |
| } elseif ($month > 9) { | |
| $newDate->modify('last day of december ' . $newDate->format('Y')); | |
| } | |
| break; | |
| case 'month': | |
| $newDate->modify('last day of this month'); | |
| break; | |
| case 'week': | |
| $newDate->modify(($newDate->format('w') === '0') ? 'now' : 'sunday this week'); | |
| break; | |
| } | |
| return $newDate->format('Y-m-d'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment