Core\Time\Date::minus
$date->minus(int $count, Core\Unit $unit): Core\Time\Date$date->minus( int $count, Core\Unit $unit, ): Core\Time\Date
Returns a new date that is a number of days, weeks, months, quarters or years earlier.
You give a count and a unit from Core\Unit, such as Core\Unit::Day. The date you call minus on does not change. It works the same way as plus in the other direction, and a negative count gives a later date.
When the day does not exist in the month of the result, the result is the last day of that month. 31 March 2024 minus one month is 29 February 2024.
Use it to count back from a day. To check that a customer is at least 18 years old, go back 18 years from today and compare the birthday with that date.
Good to know: a unit smaller than a day, such as Core\Unit::Hour, throws a LogicError.
Parameters
$countintHow many steps; negative moves forward.$unitCore\UnitUnit::Day,Unit::Week,Unit::Month,Unit::QuarterorUnit::Year.
Return value
Returns Core\Time\Date. A new Date; the receiver is unchanged.
Errors
LogicError$unitis smaller thanUnit::Day, which does not move a date.RuntimeError$countis too large for a calendar step, or the result lies outside-9999..=9999.
Examples
Days and weeks back
<?nvs// Shows how to move a date back by days and by weeks.
var $due = Core\Time\Date::at(2024, 3, 10);
// `minus` returns a new date that is 7 days earlier. This prints 2024-03-03.echo $due->minus(7, Core\Unit::Day)->format('yyyy-MM-dd'), "\n";
// A step can go back past the start of a month or a year.// Three weeks before 21 January 2024 is 2023-12-31.echo Core\Time\Date::at(2024, 1, 21)->minus(3, Core\Unit::Week)->format('yyyy-MM-dd'), "\n";
// `minus` does not change the date it is called on. This prints 2024-03-10.echo $due->format('yyyy-MM-dd'), "\n";2024-03-032023-12-312024-03-10A day the month does not have
<?nvs// Shows what `minus` does when the day does not exist in the month it reaches.
// March has 31 days, but February 2024 has only 29.// The result is the last day of February. This prints 2024-02-29.echo Core\Time\Date::at(2024, 3, 31)->minus(1, Core\Unit::Month)->format('yyyy-MM-dd'), "\n";
// 29 February exists only in a leap year. One year earlier, the result is 28 February.// This prints 2023-02-28.echo Core\Time\Date::at(2024, 2, 29)->minus(1, Core\Unit::Year)->format('yyyy-MM-dd'), "\n";
// A date has no time of day, so `Core\Unit::Hour` throws a `LogicError`.try { Core\Time\Date::at(2024, 3, 31)->minus(12, Core\Unit::Hour);} catch (LogicError $error) { echo 'Not allowed: ', $error->message, "\n";}2024-02-292023-02-28Not allowed: Core\Time\Date::minus(): a unit smaller than `Unit::Day` does not move a dateOld enough on a given day
<?nvs// Shows how to check that a customer is at least 18 years old on a given day.
// A customer is old enough if they were born on or before this date.// This prints 2006-06-15.var $today = Core\Time\Date::at(2024, 6, 15);var $latest = $today->minus(18, Core\Unit::Year);echo 'Born on or before ', $latest->format('yyyy-MM-dd'), "\n";
// `compareTo` returns 1 when a birthday is later than `$latest`.array<Core\Time\Date> $birthdays = [ Core\Time\Date::at(2006, 6, 15), Core\Time\Date::at(2006, 6, 16), Core\Time\Date::at(1990, 1, 1),];foreach ($birthdays as Core\Time\Date $born) { if ($born->compareTo($latest) > 0) { echo $born->format('yyyy-MM-dd'), ": too young\n"; } else { echo $born->format('yyyy-MM-dd'), ": old enough\n"; }}Born on or before 2006-06-152006-06-15: old enough2006-06-16: too young1990-01-01: old enough