Skip to content

Core\Time\TimeOfDay::minus

Moves a time of day back by a number of hours, minutes, seconds or smaller units, and returns the new time.

The original time does not change. A TimeOfDay has no date, so the result wraps around at midnight: 00:15 minus 30 minutes is 23:45. A negative count moves the time forward, so minus gives the same result as plus with the opposite count.

The unit must be Unit::Hour or smaller. Unit::Day and the larger units throw a LogicError, because a time of day has no days to move. A count too large for its unit throws a RuntimeError.

Parameters

  • $countintHow many steps. A negative count moves the time forward.
  • $unitCore\UnitUnit::Hour or a smaller unit.

Return value

Returns Core\Time\TimeOfDay. A new TimeOfDay. The original does not change.

Errors

  • LogicError$unit is Unit::Day or larger. A time of day has no days to move.
  • RuntimeError$count is too large for $unit.

Examples

When to leave home

<?nvs
// Shows how to get the time that is a number of minutes before another time.
Core\Time\TimeOfDay $arrive = Core\Time\TimeOfDay::at(9, 0);
// The trip takes 35 minutes. This prints 08:25.
echo $arrive->minus(35, Core\Unit::Minute)->format('HH:mm'), "\n";
// A negative count moves the time forward. This prints 09:35.
echo $arrive->minus(-35, Core\Unit::Minute)->format('HH:mm'), "\n";
Output
08:25
09:35

Back over midnight

<?nvs
// Shows that the time goes back around the clock when it passes midnight.
Core\Time\TimeOfDay $early = Core\Time\TimeOfDay::at(0, 15);
// This prints 23:45.
echo $early->minus(30, Core\Unit::Minute)->format('HH:mm'), "\n";
// One second before midnight. This prints 23:59:59.
echo Core\Time\TimeOfDay::at(0, 0)->minus(1, Core\Unit::Second)->format('HH:mm:ss'), "\n";
Output
23:45
23:59:59

Reminders before an appointment

<?nvs
// Works out when to send the reminders for an appointment.
Core\Time\TimeOfDay $appointment = Core\Time\TimeOfDay::at(14, 0);
array<int> $minutesBefore = [60, 15];
// This prints "reminder at 13:00" and "reminder at 13:45".
foreach ($minutesBefore as int $minutes) {
echo 'reminder at ', $appointment->minus($minutes, Core\Unit::Minute)->format('HH:mm'), "\n";
}
Output
reminder at 13:00
reminder at 13:45