Period.php

Create date ranges and periods of time with ease.

Installation

You can install the package via composer:

composer require gtmassey/period

Usage

All instances of Period take in Carbon instances for the startDate and endDate.

You can define a period by passing a start and end date to the static create method:

$customPeriod = Period::create(
    Carbon::now()->subDays(3), 
    Carbon::now()
);

You can also use one of the many methods provided for you to generate pre-defined periods of time:


//days
Period::today();
Period::yesterday();
Period::lastDays(int $days); //$days = 2
Period::lastDaysExcludingToday(int $days); //$days = 2

//weeks
Period::thisWeek();
Period::thisWeekExcludingToday();
Period::lastWeek();
Period::lastWeeks(int $weeks);

//months
Period::thisMonth();
Period::thisMonthExcludingToday();
Period::lastMonth();
Period::lastMonths(int $months);

//quarters (3 months)
Period::thisQuarter();
Period::thisQuarterExcludingToday();
Period::lastQuarter();
Period::lastQuarters(int $quarters);

//years
Period::thisYear();
Period::thisYearExcludingToday();
Period::lastYear();
Period::lastYears(int $years);

You can use named arguments as well:

$customPeriod = Period::create(
    startDate: Carbon::now()->subDays(3), 
    endDate: Carbon::now()
);

Testing

You can test the code by navigating to the package directory, and running the command: