Skip to content

Core\Time\DateTime::difference

$dateTime->difference(Core\Time\DateTime $other, Core\Unit $unit): int$dateTime->difference(
    Core\Time\DateTime $other,
    Core\Unit $unit,
): int

Counts the whole units from one date and time to another, such as years, months or days.

You give the other value and a unit from Core\Unit. The result is a whole number, and a part of a unit is not counted. From a birthday on 15 May 1990 to 14 May 2024 is 33 years. One day later it is 34. The result is negative when the other value is earlier.

The count uses the calendar in the time zone of the value you call difference on. The other value can be in a different time zone.

Good to know: a day is not always 24 hours. In Berlin the clocks move forward on 31 March 2024. From 12:00 on 30 March to 12:00 on 31 March is 1 day, and it is 23 hours.

Parameters

  • $otherCore\Time\DateTimeThe value to count to. It is read in this value's zone, so the two values may have different zones.
  • $unitCore\UnitThe calendar unit to count in.

Return value

Returns int. The whole count, truncated toward zero. It is negative when $other is earlier, and 0 when the two values are less than one unit apart.

Errors

  • RuntimeErrorThe count does not fit in an int. Only Unit::Nanosecond can reach this, for two values more than about 292 years apart.

Examples

The age of a person

<?nvs
// Shows how to work out the age of a person in whole years.
var $vienna = Core\Time\Zone::of('Europe/Vienna');
var $born = Core\Time\DateTime::fromIso('1990-05-15', $vienna);
// `difference` counts whole years from `$born` to the other date.
// One day before the birthday, the result is 33.
echo $born->difference(Core\Time\DateTime::fromIso('2024-05-14', $vienna), Core\Unit::Year), "\n";
// On the birthday, the result is 34.
echo $born->difference(Core\Time\DateTime::fromIso('2024-05-15', $vienna), Core\Unit::Year), "\n";
// The same span counted in months. This prints 408.
echo $born->difference(Core\Time\DateTime::fromIso('2024-05-15', $vienna), Core\Unit::Month), "\n";
Output
33
34
408

A day is not always 24 hours

<?nvs
// Shows that `difference` counts calendar days in the time zone of the value you call it on.
// In Berlin, the clocks move forward one hour on 31 March 2024.
// So the day from 30 March to 31 March is only 23 hours long.
var $berlin = Core\Time\Zone::of('Europe/Berlin');
var $start = Core\Time\DateTime::fromIso('2024-03-30 12:00', $berlin);
var $end = Core\Time\DateTime::fromIso('2024-03-31 12:00', $berlin);
// This prints 1: one whole day in Berlin.
echo $start->difference($end, Core\Unit::Day), "\n";
// This prints 23: the same span in hours.
echo $start->difference($end, Core\Unit::Hour), "\n";
// Read in UTC, 23 hours is not a whole day, so the result is 0.
var $startUtc = $start->toInstant()->in('UTC');
echo $startUtc->difference($end, Core\Unit::Day), "\n";
Output
1
23
0

Days an invoice is overdue

<?nvs
// Shows how to find the invoices that are overdue, and by how many days.
class Invoice
{
public function constructor(
public string $number,
public Core\Time\DateTime $due,
)
{}
}
var $shop = Core\Time\Zone::of('Europe/Berlin');
var $today = Core\Time\DateTime::fromIso('2024-11-20 09:00', $shop);
array<Invoice> $invoices = [
new Invoice('A-100', Core\Time\DateTime::fromIso('2024-11-04', $shop)),
new Invoice('A-101', Core\Time\DateTime::fromIso('2024-11-19', $shop)),
new Invoice('A-102', Core\Time\DateTime::fromIso('2024-12-01', $shop)),
];
// `difference` counts days from the due date to today. A-102 is due later, so its result is negative.
// This prints 16 for A-100 and 1 for A-101.
foreach ($invoices as Invoice $invoice) {
int $days = $invoice->due->difference($today, Core\Unit::Day);
if ($days > 0) {
echo $invoice->number, ' days overdue: ', $days, "\n";
}
}
Output
A-100 days overdue: 16
A-101 days overdue: 1