Skip to content

Core\Time\DateTime::minus

Returns a new date and time that is a number of units earlier, such as 3 days or 2 months.

You give a count and a unit from Core\Unit, such as Core\Unit::Week. The value you call minus on does not change, and the result is in the same time zone. It works the same way as plus in the other direction, and a negative count gives a later value.

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 fixed time, for example to send a reminder one day before an appointment.

Good to know: going back one day keeps the time of day, also when the clocks change on that day. Going back 24 hours can give a different time of day.

Parameters

  • $countintHow many steps; negative moves forward.
  • $unitCore\UnitThe calendar unit of each step.

Return value

Returns Core\Time\DateTime. A new DateTime in the same zone; the receiver is unchanged.

Errors

  • RuntimeError$count is too large for a calendar step, or the result lies outside the representable range, about ±9999 years.

Examples

Go back days hours and months

<?nvs
// Shows how to move a date and time back by days, hours and months.
var $end = Core\Time\DateTime::fromIso('2024-03-15 18:00', 'Europe/Berlin');
// `minus` returns a new value. The first argument is how many steps, and the second is the unit.
// This prints 2024-03-05 18:00.
echo $end->minus(10, Core\Unit::Day)->format('yyyy-MM-dd HH:mm'), "\n";
// This prints 2024-03-15 11:30.
echo $end->minus(390, Core\Unit::Minute)->format('yyyy-MM-dd HH:mm'), "\n";
// This prints 2023-12-15 18:00.
echo $end->minus(3, Core\Unit::Month)->format('yyyy-MM-dd HH:mm'), "\n";
// `$end` itself does not change. This prints 2024-03-15 18:00.
echo $end->format('yyyy-MM-dd HH:mm'), "\n";
Output
2024-03-05 18:00
2024-03-15 11:30
2023-12-15 18:00
2024-03-15 18:00

Month ends and clock changes

<?nvs
// Shows what `minus` does at the end of a month and when the clocks change.
// One month before 31 March is the last day of February, because February has no 31st.
// This prints 2024-02-29.
var $march = Core\Time\DateTime::fromIso('2024-03-31', 'Europe/Berlin');
echo $march->minus(1, Core\Unit::Month)->format('yyyy-MM-dd'), "\n";
// Berlin moves its clocks back one hour on 27 October 2024, so that day has 25 hours.
// One day before 27 October at 12:00 is 26 October at 12:00. This prints 2024-10-26 12:00 +02:00.
var $sunday = Core\Time\DateTime::fromIso('2024-10-27 12:00', 'Europe/Berlin');
echo $sunday->minus(1, Core\Unit::Day)->format('yyyy-MM-dd HH:mm XXX'), "\n";
// 24 hours earlier is 13:00, because one hour of that night happened twice.
// This prints 2024-10-26 13:00 +02:00.
echo $sunday->minus(24, Core\Unit::Hour)->format('yyyy-MM-dd HH:mm XXX'), "\n";
// A negative count moves forward. This prints 2024-10-28 12:00 +01:00.
echo $sunday->minus(-1, Core\Unit::Day)->format('yyyy-MM-dd HH:mm XXX'), "\n";
Output
2024-02-29
2024-10-26 12:00 +02:00
2024-10-26 13:00 +02:00
2024-10-28 12:00 +01:00

Reminders before an appointment

<?nvs
// Shows how to work out when to send reminders before an appointment.
// A patient has an appointment in New York on Monday 4 November 2024 at 10:00.
var $appointment = Core\Time\DateTime::fromIso('2024-11-04 10:00', 'America/New_York');
// Reminders go out one week, one day and two hours before it.
// This prints Monday 28 October, 10:00, then Sunday 3 November, 10:00, then Monday 4 November, 08:00.
echo 'First reminder: ', $appointment->minus(1, Core\Unit::Week)->format('EEEE d MMMM, HH:mm'), "\n";
echo 'Second reminder: ', $appointment->minus(1, Core\Unit::Day)->format('EEEE d MMMM, HH:mm'), "\n";
echo 'Last reminder: ', $appointment->minus(2, Core\Unit::Hour)->format('EEEE d MMMM, HH:mm'), "\n";
Output
First reminder: Monday 28 October, 10:00
Second reminder: Sunday 3 November, 10:00
Last reminder: Monday 4 November, 08:00