Core\Time\Date::difference
$date->difference(
Core\Time\Date $other,
Core\Unit::Day|Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::Year $unit,
): intCounts the whole days, weeks, months, quarters or years from one date to another. You choose the unit with a case of Core\Unit: Unit::Day, Unit::Week, Unit::Month, Unit::Quarter or Unit::Year.
$start->difference($end, Unit::Day) counts from $start to $end. The result is positive when $end is later and negative when $end is earlier. Only whole units count, so the result is 0 when the two dates are less than one unit apart.
A month counts when the same day of the month is reached. From 31 January to 29 February is 0 months, because February has no day 31. From 31 January to 1 March is 1 month. difference counts the same way as Core\Time\DateTime::difference, and it never throws an error.
Use Unit::Year for the age of a person, and Unit::Month for the length of a contract.
Good to know: a date has no time of day, so units smaller than Unit::Day do not compile.
Parameters
$otherCore\Time\DateThe date to count to.$unitCore\Unit::Day|Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::YearUnit::Day,Unit::Week,Unit::Month,Unit::QuarterorUnit::Year. A smaller unit does not compile.
Return value
Returns int. The whole count. It is negative when $other is earlier, and 0 when the two dates are less than one unit apart.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Time\Date::startOf and endOf return the first or last day of the date's week, month, quarter or year. difference counts whole days, weeks, months, quarters or years between two dates. next and previous find the nearest date on a given weekday. |
Examples
Days between two dates
<?nvs// Shows how to count the days and the weeks between two dates.
use Core\Unit;
var $start = Core\Time\Date::at(2024, 1, 1);var $end = Core\Time\Date::at(2024, 12, 31);
// 31 December is 365 days after 1 January, because 2024 has 366 days. This prints 365.echo $start->difference($end, Unit::Day), "\n";
// Only whole weeks count. This prints 52.echo $start->difference($end, Unit::Week), "\n";
// Counting back to an earlier date gives a negative number. This prints -365.echo $end->difference($start, Unit::Day), "\n";36552-365The age of a person
<?nvs// Shows how to work out the age of a person from the date of birth.
class Person{ // The age is the number of whole years from the birthday to today. public static function age(Core\Time\Date $born, Core\Time\Date $today): int { return $born->difference($today, Core\Unit::Year); }}
var $born = Core\Time\Date::at(1990, 6, 15);
// The day before the birthday, the person is still 35. This prints 35.echo Person::age($born, Core\Time\Date::at(2026, 6, 14)), "\n";
// On the birthday, the person is 36. This prints 36.echo Person::age($born, Core\Time\Date::at(2026, 6, 15)), "\n";3536Only whole months count
<?nvs// Shows how `difference` counts months when the months have different lengths.
use Core\Unit;
var $start = Core\Time\Date::at(2024, 1, 31);
// February has no day 31, so a whole month is not reached. This prints 0.echo $start->difference(Core\Time\Date::at(2024, 2, 29), Unit::Month), "\n";
// On 1 March, one whole month is reached. This prints 1.echo $start->difference(Core\Time\Date::at(2024, 3, 1), Unit::Month), "\n";
// A quarter is three months. This prints 1.echo $start->difference(Core\Time\Date::at(2024, 5, 1), Unit::Quarter), "\n";011