Get the First or Last Day of a Week, Month, Quarter or Year in PHP
Get the First or Last Day of a Week, Month, Quarter or Year in PHP is a slight retouched of @davgothic First/Last Day Period 2013 Work., (*1)
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 functions used returns a DateTime object, so you can output the date in any format available to the PHP date() function., (*2)
Please see CodePad for a demo., (*3)
PHP >= 5.2, (*4)
$ composer require ikunyemingor/first-or-last-period-date
OR, (*5)
{ "require": { "ikunyemingor/first-or-last-period-date": ">=0.1.1" } }
```php firstDayOf('week'); echo 'The first day of the current week is: ' . $date->format('l, jS F Y') . "\n"; // Get current week last day. $date = $pd->lastDayOf('week'); echo 'The last day of the current week is: ' . $date->format('l, jS F Y') . "\n\n"; // Get first day of each week between two dates. print_r($pd->getWeekFirstDayBetweenDates("2018-01-08", date('Y-m-d'))); ?>, (*6)
### Without Composer - Basic Usage ```php firstDayOf('week'); echo 'The first day of the current week is: ' . $date->format('l, jS F Y') . "\n"; // Get current week last day. $date = $pd->lastDayOf('week'); echo 'The last day of the current week is: ' . $date->format('l, jS F Y') . "\n\n"; // Get first day of each week between two dates. print_r($pd->getWeekFirstDayBetweenDates("2018-01-08", date('Y-m-d'))); // Get all day dates between two dates. print_r($pd->getDayDatesBetweenTwoDates("2018-01-08", date('Y-m-d'))); ?>
firstDayOf('week'); echo 'The first day of the current week is: ' . $date->format('l, jS F Y') . "\n"; $date = $pd->lastDayOf('week'); echo 'The last day of the current week is: ' . $date->format('l, jS F Y') . "\n\n"; // Get current month first and last day. $date = $pd->firstDayOf('month'); echo 'The first day of the current month is: ' . $date->format('l, jS F Y') . "\n"; $date = $pd->lastDayOf('month'); echo 'The last day of the current month is: ' . $date->format('l, jS F Y') . "\n\n"; // Get current year first and last day. $date = $pd->firstDayOf('year'); echo 'The first day of the current year is: ' . $date->format('l, jS F Y') . "\n"; $date = $pd->lastDayOf('year'); echo 'The last day of the current year is: ' . $date->format('l, jS F Y') . "\n\n"; // Get yesterday. echo 'Yesterday is: ' . date("l, jS F Y", strtotime("yesterday")) . "\n\n"; // Get previous week first and last day. $specifiedDate = new DateTime(date('Y')); $date = $pd->firstDayOf('week', $specifiedDate, "last"); echo 'The first day of the previous week is: ' . $date->format('l, jS F Y') . "\n"; $date = $pd->lastDayOf('week', $specifiedDate, "last"); echo 'The last day of the previous week is: ' . $date->format('l, jS F Y') . "\n\n"; // Get previous month first and last day. $specifiedDate = new DateTime(date('Y')); $date = $pd->firstDayOf('month', $specifiedDate, "last"); echo 'The first day of the previous month is: ' . $date->format('l, jS F Y') . "\n"; $date = $pd->lastDayOf('month', $specifiedDate, "last"); echo 'The last day of the previous month is: ' . $date->format('l, jS F Y') . "\n\n"; // Get previous year first and last day. $specifiedDate = new DateTime(date('Y') - 1); $date = $pd->firstDayOf('year', $specifiedDate, "last"); echo 'The first day of the previous year is: ' . $date->format('l, jS F Y') . "\n"; $date = $pd->lastDayOf('year', $specifiedDate, "last"); echo 'The last day of the previous year is: ' . $date->format('l, jS F Y') . "\n\n"; // Get first day of each week between two dates with first day of week as Monday and custom returned date format. print_r($pd->getWeekFirstDayBetweenDates("2018-01-08", date('Y-m-d'), "1", date('l, jS F Y'))); // Get all day dates between two dates with an interval and custom returned date format. print_r($pd->getDayDatesBetweenTwoDates("2018-11-01", date('Y-m-d'), 1, date('l, jS F Y'))); ?>
View change log here, (*7)
Ikunyemi Ngor, (*8)
Released under the Apache License 2.0, (*9)