2017 © Pedro Peláez
 

package first-or-last-period-date

Get the First or Last Day of a Week, Month, Quarter or Year in PHP

image

ikunyemingor/first-or-last-period-date

Get the First or Last Day of a Week, Month, Quarter or Year in PHP

  • Friday, June 22, 2018
  • by ikunyemingor
  • Repository
  • 0 Watchers
  • 1 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

First or Last Period Date

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)

Demo and Examples

Please see CodePad for a demo., (*3)

Installation

Requirements

PHP >= 5.2, (*4)

With Composer - Basic Usage

$ 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'))); ?>

More Examples

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')));

?>

Changes

View change log here, (*7)

Contributors

Ikunyemi Ngor, (*8)

License

Released under the Apache License 2.0, (*9)

The Versions

22/06 2018

dev-master

9999999-dev

Get the First or Last Day of a Week, Month, Quarter or Year in PHP

  Sources   Download

Apache-2.0

The Requires

  • php >=5.2

 

by Ikunyemi Ngor

php datetime