Core\Unit
Core\Unit is a step in the calendar or on the clock: a nanosecond, a microsecond, a millisecond, a second, a minute, an hour, a day, a week, a month, a quarter or a year.
You give a Unit to the methods that move a date forward or back, such as plus and minus. You also give one to startOf and endOf, and to difference, which counts the units between two dates. The cases are in order from the smallest to the largest. A week starts on Monday.
Good to know: a calendar step is not always the same length of time. One month after 31 January is the last day of February, because February has no day 31. In a time zone with summer time, the clocks move forward one hour on one day of the year, so that day has 23 hours. For a length of time that never changes, such as a timeout, use a Core\Time\Duration.
Examples
The eleven steps a date can move by
<?nvs// This program adds one of each of the eleven units to the same date and time.// The units go from smallest to largest.
var $utc = Core\Time\Zone::UTC;var $start = Core\Time\DateTime::fromIso('2024-01-31 09:00', $utc);var $fmt = 'yyyy-MM-dd HH:mm:ss.SSSSSSSSS';
array<Core\Unit> $steps = [ 'nanosecond' => Core\Unit::Nanosecond, 'microsecond' => Core\Unit::Microsecond, 'millisecond' => Core\Unit::Millisecond, 'second' => Core\Unit::Second, 'minute' => Core\Unit::Minute, 'hour' => Core\Unit::Hour, 'day' => Core\Unit::Day, 'week' => Core\Unit::Week, 'month' => Core\Unit::Month, 'quarter' => Core\Unit::Quarter, 'year' => Core\Unit::Year,];
echo 'starting from ', $start->format($fmt), "\n\n";
foreach ($steps as string $name => Core\Unit $step) { echo 'one ', $name, ' later: ', $start->plus(1, $step)->format($fmt), "\n";}
// One month after 31 January is 29 February, because 31 February does not exist.// The day changes to the last day of the new month.starting from 2024-01-31 09:00:00.000000000
one nanosecond later: 2024-01-31 09:00:00.000000001one microsecond later: 2024-01-31 09:00:00.000001000one millisecond later: 2024-01-31 09:00:00.001000000one second later: 2024-01-31 09:00:01.000000000one minute later: 2024-01-31 09:01:00.000000000one hour later: 2024-01-31 10:00:00.000000000one day later: 2024-02-01 09:00:00.000000000one week later: 2024-02-07 09:00:00.000000000one month later: 2024-02-29 09:00:00.000000000one quarter later: 2024-04-30 09:00:00.000000000one year later: 2025-01-31 09:00:00.000000000A month is not thirty days
<?nvs// This program shows that calendar units have no fixed length.// A month has 28 to 31 days, and a leap year has one extra day.// A day has 23 or 25 hours when the clocks change.
var $utc = Core\Time\Zone::UTC;var $day = 'yyyy-MM-dd';
// Adding months keeps the day number if the new month has it.// If not, the result is the last day of that month.var $endOfJanuary = Core\Time\DateTime::fromIso('2024-01-31', $utc);echo '31 January plus one month: ', $endOfJanuary->plus(1, Core\Unit::Month)->format($day), "\n";echo '31 January plus two months: ', $endOfJanuary->plus(2, Core\Unit::Month)->format($day), "\n";
// Going back one month starts from 29 February, so the result is 29 January.// The date you started from was 31 January.var $there = $endOfJanuary->plus(1, Core\Unit::Month);echo 'and back one month again: ', $there->minus(1, Core\Unit::Month)->format($day), "\n\n";
// 2025 has no 29 February, so the result is 28 February.var $leapDay = Core\Time\DateTime::fromIso('2024-02-29', $utc);echo '29 February plus one year: ', $leapDay->plus(1, Core\Unit::Year)->format($day), "\n\n";
// The clocks go forward in Berlin on 31 March 2024, so that day has 23 hours.// The same day measured in days is still `1`.var $berlin = Core\Time\Zone::of('Europe/Berlin');var $morning = Core\Time\DateTime::fromIso('2024-03-31', $berlin)->startOf(Core\Unit::Day);var $nextMorning = $morning->plus(1, Core\Unit::Day);echo '31 March in Berlin is ', $morning->difference($nextMorning, Core\Unit::Hour), " hours long\n";echo 'and still ', $morning->difference($nextMorning, Core\Unit::Day), " day long\n";31 January plus one month: 2024-02-2931 January plus two months: 2024-03-31and back one month again: 2024-01-29
29 February plus one year: 2025-02-28
31 March in Berlin is 23 hours longand still 1 day longClosing the books for a quarter
<?nvs// This program finds the invoices for a quarterly report.// `startOf` and `endOf` with `Core\Unit::Quarter` give the first and last day of the quarter.// `difference` in days shows which invoices are more than 30 days old.
var $utc = Core\Time\Zone::UTC;var $day = 'yyyy-MM-dd';var $today = Core\Time\DateTime::fromIso('2024-08-20', $utc);
var $from = $today->startOf(Core\Unit::Quarter);var $to = $today->endOf(Core\Unit::Quarter);echo 'reporting ', $from->format($day), ' .. ', $to->format($day), "\n\n";
array<Core\Time\DateTime> $issued = [ 'INV-1041' => Core\Time\DateTime::fromIso('2024-06-28', $utc), 'INV-1042' => Core\Time\DateTime::fromIso('2024-07-02', $utc), 'INV-1043' => Core\Time\DateTime::fromIso('2024-07-19', $utc), 'INV-1044' => Core\Time\DateTime::fromIso('2024-08-12', $utc),];
uint $inThisQuarter = 0;
foreach ($issued as string $number => Core\Time\DateTime $date) { // An invoice belongs to the quarter when its date is between the first and last day. bool $covered = $from->difference($date, Core\Unit::Day) >= 0 && $date->difference($to, Core\Unit::Day) >= 0; if ($covered) { $inThisQuarter = $inThisQuarter + 1; }
int $age = $date->difference($today, Core\Unit::Day); echo $number, ' issued ', $date->format($day), ', ', $age, ' days ago', $covered ? '' : ', belongs to the quarter before', $age > 30 ? ' — chase it' : '', "\n";}
echo "\n", $inThisQuarter, " invoice(s) go into this quarter's report\n";reporting 2024-07-01 .. 2024-09-30
INV-1041 issued 2024-06-28, 53 days ago, belongs to the quarter before — chase itINV-1042 issued 2024-07-02, 49 days ago — chase itINV-1043 issued 2024-07-19, 32 days ago — chase itINV-1044 issued 2024-08-12, 8 days ago
3 invoice(s) go into this quarter's report