Core\Time\DateTime::hour
$dateTime->hour(): uintReturns the hour of a date and time, as a uint from 0 to 23.
Midnight is 0 and 23:00 is 23. The hour is read in the time zone of the value. At 08:00 UTC, hour returns 9 in Berlin, 3 in New York and 17 in Tokyo.
The result has the same name and type as the hour option of with. So $d->with({ hour: $d->hour() + 1 }) gives the same day one hour later on the clock. The hour option must be from 0 to 23, so at hour 23 this throws a RuntimeError.
Good to know: to show a time to a user, first convert it to the user's zone with in($zone). Then read the hour.
Return value
Returns uint. The hour, from 0 to 23.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Time\DateTime::year, Core\Time\DateTime::month, Core\Time\DateTime::day, Core\Time\DateTime::hour, Core\Time\DateTime::minute, Core\Time\DateTime::second and Core\Time\DateTime::nanoseconds return the parts of a date and time as numbers. Each one reads the clock in the zone of the value. Core\Time\DateTime::year returns an int, and the others return a uint. |
Examples
Read the hour
<?nvs// Shows how to read the hour of a date and time as a number.
var $meeting = Core\Time\DateTime::fromIso('2024-03-05 14:30', 'Europe/Berlin');
// `hour` returns a `uint` from 0 to 23. This prints 14.echo $meeting->hour(), "\n";
// Midnight is hour 0. This prints 0.echo Core\Time\DateTime::fromIso('2024-03-06', 'Europe/Berlin')->hour(), "\n";140A greeting for each user
<?nvs// Shows how to greet each user by the time of day in the user's own time zone.
class Greeting{ public static function choose(Core\Time\DateTime $local): string { uint $hour = $local->hour(); if ($hour < 5) { return 'Good night'; } if ($hour < 12) { return 'Good morning'; } if ($hour < 18) { return 'Good afternoon'; } return 'Good evening'; }}
// All three users open the page at the same moment, 08:00 UTC.var $moment = Core\Time\Instant::fromIso('2024-03-05T08:00:00Z');array<string> $zones = ['Europe/Berlin', 'America/New_York', 'Asia/Tokyo'];
// The hour is 9 in Berlin, 3 in New York and 17 in Tokyo, so each user gets a different greeting.foreach ($zones as string $name) { var $local = $moment->in(Core\Time\Zone::of($name)); echo $name, ' (', $local->hour(), '): ', Greeting::choose($local), "\n";}Europe/Berlin (9): Good morningAmerica/New_York (3): Good nightAsia/Tokyo (17): Good afternoonIs the support desk open
<?nvs// Shows how to check if a support desk is open, using the hour in the desk's time zone.
class SupportDesk{ // The desk in London is open from 09:00 to 17:00. public static function isOpen(Core\Time\Instant $at): bool { uint $hour = $at->in('Europe/London')->hour(); return $hour >= 9 && $hour < 17; }}
// A real program uses `Core\Time::now()` here. Fixed times keep the output the same.// In July, London is one hour ahead of UTC. 07:30 UTC is 08:30 in London, so the desk is closed.array<string> $times = ['2024-07-10T07:30:00Z', '2024-07-10T08:30:00Z', '2024-07-10T16:10:00Z'];foreach ($times as string $text) { echo $text, ': ', SupportDesk::isOpen(Core\Time\Instant::fromIso($text)) ? 'open' : 'closed', "\n";}2024-07-10T07:30:00Z: closed2024-07-10T08:30:00Z: open2024-07-10T16:10:00Z: closed