Skip to content

Core\Time\DateTime::timeOfDay

$dateTime->timeOfDay(): Core\Time\TimeOfDay

Returns the time of day of a date and time, as a Core\Time\TimeOfDay.

The result has the hour, the minute, the second and the nanoseconds. It has no date and no time zone. Use it to compare a value with a fixed time, for example to check whether a message arrived during opening hours. You can also give the result to withTime to copy the time to another day.

Good to know: timeOfDay reads the clock in the time zone of the value. 12:00 in UTC on a summer day is 14:00 in Berlin and 08:00 in New York. To get the time for a certain place, convert the value to that time zone first.

Return value

Returns Core\Time\TimeOfDay. The TimeOfDay component, hour through nanosecond.

Examples

The time of a date and time

<?nvs
// Shows how to take the time of day out of a date and time.
var $meeting = Core\Time\DateTime::fromIso('2024-03-05 14:30', 'Europe/Berlin');
// `timeOfDay` returns a `Core\Time\TimeOfDay` with only the hour, minute, second and nanoseconds.
// This prints 14:30:00.
var $clock = $meeting->timeOfDay();
echo $clock->format('HH:mm:ss'), "\n";
// The date is gone, so the result is equal to a time built from the same numbers.
// This prints 0.
echo $clock->compareTo(Core\Time\TimeOfDay::at(14, 30)), "\n";
// A `TimeOfDay` has no date, so adding time goes around the clock.
// This prints 00:30.
echo $clock->plus(10, Core\Unit::Hour)->format('HH:mm'), "\n";
Output
14:30:00
0
00:30

The time depends on the time zone

<?nvs
// Shows that one moment has a different time of day in each time zone.
var $start = Core\Time\Instant::fromIso('2024-06-10T12:00:00Z');
// `timeOfDay` reads the clock in the time zone of the value.
// This prints 14:00 for Berlin, 08:00 for New York and 17:30 for Kolkata.
array<string> $zones = ['Europe/Berlin', 'America/New_York', 'Asia/Kolkata'];
foreach ($zones as string $zone) {
var $local = $start->in(Core\Time\Zone::of($zone));
echo $zone, ': ', $local->timeOfDay()->format('HH:mm'), "\n";
}
Output
Europe/Berlin: 14:00
America/New_York: 08:00
Asia/Kolkata: 17:30

Is the support desk open

<?nvs
// Shows how to check whether a message arrived during a support desk's opening hours.
// The desk in Vienna is open from 09:00 to 17:30, local time.
var $desk = Core\Time\Zone::of('Europe/Vienna');
var $opens = Core\Time\TimeOfDay::at(9, 0);
var $closes = Core\Time\TimeOfDay::at(17, 30);
// The server saves each message time in UTC.
array<string> $received = [
'2024-11-04T07:45:00Z',
'2024-11-04T12:10:00Z',
'2024-11-04T16:29:00Z',
'2024-11-04T16:31:00Z',
];
// Vienna is one hour ahead of UTC in November. `compareTo` returns -1, 0 or 1.
// This prints closed, open, open and closed.
foreach ($received as string $text) {
var $clock = Core\Time\Instant::fromIso($text)->in($desk)->timeOfDay();
string $state = 'closed';
if ($clock->compareTo($opens) >= 0 && $clock->compareTo($closes) < 0) {
$state = 'open';
}
echo $clock->format('HH:mm'), ' ', $state, "\n";
}
Output
08:45 closed
13:10 open
17:29 open
17:31 closed