If you’ve ever needed to find the first or last day of a given period and you’re rocking a PHP version greater than or equal to 5.2, today is your lucky day!
Both of these functions return a DateTime object, so you can output the date in any format available to the PHP date() function.
Get the First Day of a Week, Month, Quarter or Year
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/** * Return the first day of the Week/Month/Quarter/Year that the * current/provided date falls within * * @param string $period The period to find the first day of. ('year', 'quarter', 'month', 'week') * @param DateTime $date The date to use instead of the current date * * @return DateTime * @throws InvalidArgumentException */ 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; } |
Get the Last Day of a Week, Month, Quarter or Year
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/** * Return the last day of the Week/Month/Quarter/Year that the * current/provided date falls within * * @param string $period The period to find the last day of. ('year', 'quarter', 'month', 'week') * @param DateTime $date The date to use instead of the current date * * @return DateTime * @throws InvalidArgumentException */ 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; } |
Here is a usage example of both functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Get the first day of the current week $date = firstDayOf('week'); echo 'The first day of the current week is: ' . $date->format('l, jS F Y') . "\n"; // Get the last day of the current month $date = lastDayOf('month'); echo 'The last day of the current month is: ' . $date->format('l, jS F Y') . "\n"; // Get the first day of the specified month $specifiedDate = new DateTime('2011-08-30'); $date = firstDayOf('week', $specifiedDate); echo 'The first day of the week in which ' . $specifiedDate->format('l, jS F Y') . ' falls within is: ' . $date->format('l, jS F Y') . "\n"; // Get the last day of the specified quarter $specifiedDate = new DateTime('2013-08-30'); $date = lastDayOf('quarter', $specifiedDate); echo 'The last day of the quarter in which ' . $specifiedDate->format('l, jS F Y') . ' falls within is: ' . $date->format('l, jS F Y') . "\n"; |
This is the output of the above usage example when run today:
1 2 3 4 |
The first day of the current week is: Monday, 5th August 2013 The last day of the current month is: Saturday, 31st August 2013 The first day of the week in which Tuesday, 30th August 2011 falls within is: Monday, 29th August 2011 The last day of the quarter in which Friday, 30th August 2013 falls within is: Monday, 30th September 2013 |
I hope you find these functions useful!