Skip to content

Core\Time\TimeOfDay::at

Core\Time\TimeOfDay::at(
    uint $hour,
    uint $minute,
    {second?: uint, nanoseconds?: uint},
): Core\Time\TimeOfDay

Makes a time of day from its parts: an hour, a minute and, if you need them, a second and nanoseconds.

A TimeOfDay is a time on a clock, such as 10:30. It has no date and no time zone. Use it for times that repeat every day, like opening hours or the time a daily job runs.

The hour goes from 0 to 23, and the minute and the second go from 0 to 59. The second and the nanoseconds are optional, and their default is 0. When a part is outside its range, at throws a RuntimeError. So 24:00 is not a time of day: midnight is hour 0 and minute 0.

Parameters

  • $houruintThe hour, 0 to 23.
  • $minuteuintThe minute, 0 to 59.

Options

  • seconduintoptionalThe second, 0 to 59. The default is 0.
  • nanosecondsuintoptionalThe nanoseconds inside that second, 0 to 999999999. The default is 0.

Return value

Returns Core\Time\TimeOfDay. The new TimeOfDay.

Errors

  • RuntimeErrorA value is outside its range, for example an hour of 24.

Examples

An opening time

<?nvs
// Shows how to make a time of day from an hour and a minute.
Core\Time\TimeOfDay $opens = Core\Time\TimeOfDay::at(9, 0);
// The seconds are `0` when you leave them out. This prints 09:00:00.
echo $opens->format('HH:mm:ss'), "\n";
// You can give the seconds by name. This prints 09:00:30.
echo Core\Time\TimeOfDay::at(9, 0, { second: 30 })->format('HH:mm:ss'), "\n";
Output
09:00:00
09:00:30

A time that does not exist

<?nvs
// Shows what happens when a part of the time is outside its range.
// The hours of a day go from 0 to 23, so hour 24 throws a `RuntimeError`.
try {
Core\Time\TimeOfDay::at(24, 0);
} catch (RuntimeError $e) {
echo $e->message, "\n";
}
// Midnight at the start of the day is hour 0. This prints 00:00.
echo Core\Time\TimeOfDay::at(0, 0)->format('HH:mm'), "\n";
Output
Core\Time\TimeOfDay::at(): `24:0:0.0` is not a time of day
00:00

The time a daily backup runs

<?nvs
// Sets the time of a daily backup, and checks if today's backup is still to come.
Core\Time\TimeOfDay $backup = Core\Time\TimeOfDay::at(2, 30);
// A real program uses `Core\Time::now()` here. A fixed time keeps the output the same.
array<string> $checks = ['2024-03-01T01:50:00Z', '2024-03-01T02:30:00Z', '2024-03-01T23:10:00Z'];
foreach ($checks as string $at) {
Core\Time\TimeOfDay $now = Core\Time\Instant::fromIso($at)->in('UTC')->timeOfDay();
// This prints "still to come" once, and then "done for today" twice.
if ($now < $backup) {
echo $now->format('HH:mm'), " still to come\n";
} else {
echo $now->format('HH:mm'), " done for today\n";
}
}
Output
01:50 still to come
02:30 done for today
23:10 done for today