ikunyemingor/first-or-last-period-date

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

dev-master / 0.1.x-dev 2019-01-25 13:09 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:42:02 UTC


README

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.

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.

Demo and Examples

Please see CodePad for a demo.

Installation

Requirements

PHP >= 5.2

With Composer - Basic Usage

$ composer require ikunyemingor/first-or-last-period-date

OR

{
    "require": {
        "ikunyemingor/first-or-last-period-date": ">=0.1.1"
    }
}
<?php

require 'vendor/autoload.php';

use LostCodes\PeriodDate;

$pd = new PeriodDate();

// Get current week first day.
$date = $pd->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')));

?>

Without Composer - Basic Usage

<?php

use LostCodes\PeriodDate;

require_once "includes/PeriodDate.php";

$pd = new PeriodDate();

// Get current week first day.
$date = $pd->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

<?php

$pd = new PeriodDate();
// Get today.
echo 'Today is: ' . date("l, jS F Y", strtotime("today")) . "\n\n";
// Get current week first and last day.
$date = $pd->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

Contributors

Ikunyemi Ngor

License

Released under the Apache License 2.0