Core\Time\DateTime::quarter
$dateTime->quarter(): uintReturns the quarter of the year that contains this date and time, from 1 to 4.
Quarter 1 is January to March, 2 is April to June, 3 is July to September and 4 is October to December. The month is the month in the value's own time zone. One moment can be 31 March in New York and 1 April in Berlin, so the result can depend on the time zone.
Use quarter to group orders, invoices or reports by quarter. The pattern letter Q in format prints the same number as text.
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
| Version | Change |
|---|---|
0.0.6 | A 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 the year for a few dates.
// January to March is quarter 1, and October to December is quarter 4.// This prints 1, 2, 3 and 4.array<string> $dates = ['2026-03-31', '2026-04-01', '2026-09-15', '2026-12-24'];foreach ($dates as string $text) { echo $text, ': ', Core\Time\DateTime::fromIso($text, 'UTC')->quarter(), "\n";}2026-03-31: 12026-04-01: 22026-09-15: 32026-12-24: 4The quarter depends on the time zone
<?nvs// Shows that the quarter is read in the value's own time zone.
// 22:00 on 31 March in New York is 04:00 on 1 April in Berlin.var $newYork = Core\Time\DateTime::fromIso('2026-03-31 22:00', 'America/New_York');var $berlin = $newYork->toInstant()->in(Core\Time\Zone::of('Europe/Berlin'));
// The same moment is in quarter 1 in New York and in quarter 2 in Berlin. This prints 1, then 2.echo $newYork->quarter(), "\n";echo $berlin->quarter(), "\n";12Sales per quarter
<?nvs// Adds up the sales of a shop for each quarter of the year.
array<string> $orderTimes = ['2026-01-12 10:00', '2026-02-03 16:45', '2026-05-20 09:15', '2026-11-30 18:00'];array<int> $amounts = [40, 25, 60, 15];
// `quarter` returns 1 to 4. The total of quarter 1 is at index 0 of `$totals`.array<int> $totals = [0, 0, 0, 0];int $i = 0;foreach ($orderTimes as string $text) { uint $q = Core\Time\DateTime::fromIso($text, 'Europe/Berlin')->quarter(); $totals[($q - 1) as string] = $totals[($q - 1) as string] + $amounts[$i as string]; $i = $i + 1;}
// This prints Q1: 65, Q2: 60, Q3: 0 and Q4: 15.int $n = 0;while ($n < 4) { echo 'Q', $n + 1, ': ', $totals[$n as string], "\n"; $n = $n + 1;}Q1: 65Q2: 60Q3: 0Q4: 15