Skip to content
Advertisement

PHP how do I get previous Quarter start and end date

I know how to get the previous Quarter number how to turn that into date ranges especially when it goes into the previous year?

$Quarter = floor((date('n') - 1) / 3);

Advertisement

Answer

Here you go:

function getQuarter(DateTime $DateTime) {
    $y = $DateTime->format('Y');
    $m = $DateTime->format('m');
    switch($m) {
        case $m >= 1 && $m <= 3:
            $start = '01/01/'.$y;
            $end = (new DateTime('03/1/'.$y))->modify('Last day of this month')->format('m/d/Y');
            $title = 'Q1 '.$y;
            break;
        case $m >= 4 && $m <= 6:
            $start = '04/01/'.$y;
            $end = (new DateTime('06/1/'.$y))->modify('Last day of this month')->format('m/d/Y');
            $title = 'Q2 '.$y;
            break;
        case $m >= 7 && $m <= 9:
            $start = '07/01/'.$y;
            $end = (new DateTime('09/1/'.$y))->modify('Last day of this month')->format('m/d/Y');
            $title = 'Q3 '.$y;
            break;
        case $m >= 10 && $m <= 12:
            $start = '10/01/'.$y;
            $end = (new DateTime('12/1/'.$y))->modify('Last day of this month')->format('m/d/Y');
            $title = 'Q4 '.$y;
            break;
    }
    return array(
            'start' => $start,
            'end' => $end,
            'title'=>$title,
            'start_nix' => strtotime($start),
            'end_nix' => strtotime($end)
    );
}

print_r(getQuarter(new DateTime()));

Output

Array
(
    [start] => 10/01/2018
    [end] => 12/31/2018
    [title] => Q4 2018
    [start_nix] => 1538377200
    [end_nix] => 1546243200
)

Sandbox

Your in luck I wrote this a wile ago … This is sort of the brute force way of doing it, but hey it works. There is probably a “fancier” way, but whatever…

UPDATE

Based on some comments using DateTime has many advantages, besides just making the code in the function more concise. For example to get a previous quarter:

print_r(getQuarter((new DateTime())->modify('-3 Months')); 

Output

Array
(
    [start] => 07/01/2018
    [end] => 09/30/2018
    [title] => Q3 2018
    [start_nix] => 1530428400
    [end_nix] => 1538290800
)

Sandbox

Here the extra parentheses are important (around new DateTime)

(new DateTime())->modify('-3 Months');

This causes the constructor to return the instance of the object, which lets you immediately call modify on it. It’s equivalent to doing this:

$DateTime = new DateTime();
$DateTime->modify('-3 Months');

But without creating a local variable.

And on the same token you can get the next quarter by doing

print_r(getQuarter((new DateTime())->modify('+3 Months'));

Another example of this is in the function itself (specifically):

(new DateTime('03/1/'.$y))->modify('Last day of this month')

What this does is get the last day of whatever month the DateTime object has, in this case it’s 3. So we don’t have to even think of how many days that month has, it just returns the correct number. These are Relative Date formats

http://php.net/manual/en/datetime.formats.relative.php

One last one that may be of use to you is this one first day of ? this year where the ? is the month name. For example:

 print_r(getQuarter((new DateTime())->modify('first day of january this year')));
 print_r(getQuarter((new DateTime())->modify('first day of april this year')));
 print_r(getQuarter((new DateTime())->modify('first day of july this year')));
 print_r(getQuarter((new DateTime())->modify('first day of october this year')));

Effectively this will give you each quarter this year.

Hope that helps.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement