Skip to content

Core\Time\Date::quarter

$date->quarter(): uint

Returns the quarter of the year that contains a date, as a number from 1 to 4.

A quarter is three months. Quarter 1 is January to March, 2 is April to June, 3 is July to September and 4 is October to December. The result is a uint, and quarter never throws an error.

Use it to group sales or invoices by quarter. startOf(Unit::Quarter) and endOf(Unit::Quarter) return the first and the last day of the quarter.

Good to know: format with QQQ prints the quarter as text, such as Q2.

Return value

Returns uint. 1 for January to March, 2 for April to June, 3 for July to September and 4 for October to December.

Changelog

VersionChange
0.0.6A Date and a DateTime return four more calendar parts. daysInMonth() is the number of days in the month, and quarter() is 1 to 4. isoWeek() and isoWeekYear() are the week number and its year in ISO 8601, where a week starts on Monday.

Examples

The quarter of a date

<?nvs
// Shows the quarter of a few dates in one year.
// January to March is quarter 1. This prints 1.
echo Core\Time\Date::at(2026, 3, 31)->quarter(), "\n";
// April to June is quarter 2. This prints 2.
echo Core\Time\Date::at(2026, 4, 1)->quarter(), "\n";
// October to December is quarter 4. This prints 4.
echo Core\Time\Date::at(2026, 12, 31)->quarter(), "\n";
Output
1
2
4

Sales by quarter

<?nvs
// Shows how to add up sales for each quarter of a year.
array<string> $days = ['2026-01-15', '2026-02-03', '2026-05-20', '2026-08-01', '2026-11-11', '2026-12-24'];
array<int> $amounts = [120, 80, 200, 50, 90, 310];
array<int> $totals = [0, 0, 0, 0];
int $i = 0;
while ($i < 6) {
// Quarter 1 goes into the first total, so the index is the quarter minus 1.
int $index = Core\Time\Date::fromIso($days[$i])->quarter() as int - 1;
$totals[$index] = $totals[$index] + $amounts[$i];
$i = $i + 1;
}
// This prints 200, 200, 50 and 400.
echo 'Q1 ', $totals[0], "\n";
echo 'Q2 ', $totals[1], "\n";
echo 'Q3 ', $totals[2], "\n";
echo 'Q4 ', $totals[3], "\n";
Output
Q1 200
Q2 200
Q3 50
Q4 400

The dates of a quarter

<?nvs
// Shows how to print the quarter of a date with its first and its last day.
var $date = Core\Time\Date::at(2026, 8, 17);
// This prints Q3 2026: 2026-07-01 to 2026-09-30.
echo 'Q', $date->quarter(), ' ', $date->year(), ': ',
$date->startOf(Core\Unit::Quarter)->toIso(), ' to ',
$date->endOf(Core\Unit::Quarter)->toIso(), "\n";
Output
Q3 2026: 2026-07-01 to 2026-09-30