Core\Time\DateTime::at
Core\Time\DateTime::at(
Core\Time\Date $date,
Core\Time\TimeOfDay $time,
Core\Time\Zone $zone,
): Core\Time\DateTimeBuilds a date and time from three parts: a Date, a TimeOfDay and a time zone.
The result is a DateTime, which is always in a zone, so the same call gives the same moment on every server. TimeOfDay::at(0, 0) is midnight. When every part is a fixed value, you can also write DateTime::fromIso('2024-03-05 09:30', 'Europe/Berlin').
A date that does not exist, such as 30 February, throws a RuntimeError in Date::at. A clock time the zone skips is different. When clocks jump from 02:00 to 03:00, the time 02:30 moves forward to 03:30, and there is no error.
The examples below build a date with and without a time, show what happens to a date or time that does not exist, and show one meeting time in the zones of the people who join it.
Parameters
$dateCore\Time\DateThe date, such asDate::at(2024, 3, 1).$timeCore\Time\TimeOfDayThe time of day, such asTimeOfDay::at(9, 30).$zoneCore\Time\ZoneThe zone the date and the time are read in.
Return value
Returns Core\Time\DateTime. The DateTime in $zone.
Errors
RuntimeErrorThe time is outside-9999-01-02T01:59:59Z..=9999-12-30T22:00:00Z. This can only happen on the first or the last day of the range.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Time\DateTime::fromIso reads a local date and time, such as 2024-03-01 09:30, in the zone you give. Core\Time\DateTime::at builds a DateTime from a Date, a TimeOfDay and a zone. Core\Time\DateTime::parse reads text in a pattern you choose. |
Examples
A date and a time of day
<?nvs// Shows how to build a date and time from a date, a time of day and a time zone.
var $date = Core\Time\Date::at(2024, 3, 5);
// `TimeOfDay::at(0, 0)` is midnight. This prints 2024-03-05 00:00.echo Core\Time\DateTime::at($date, Core\Time\TimeOfDay::at(0, 0), 'Europe/Berlin')->format('yyyy-MM-dd HH:mm'), "\n";
// The time of day can also have seconds. This prints 2024-03-05 09:30:15 +01:00.var $time = Core\Time\TimeOfDay::at(9, 30, { second: 15 });echo Core\Time\DateTime::at($date, $time, 'Europe/Berlin')->format('yyyy-MM-dd HH:mm:ss XXX'), "\n";2024-03-05 00:002024-03-05 09:30:15 +01:00A date that does not exist
<?nvs// Shows what happens with a date or a time that does not exist.
// February 2023 has 28 days, so `Date::at` throws a `RuntimeError` for day 29.try { Core\Time\Date::at(2023, 2, 29);} catch (RuntimeError $error) { echo 'Not a date: ', $error->message, "\n";}
// On 31 March 2024, clocks in Berlin jump from 02:00 to 03:00.// 02:30 is skipped, so the result is 03:30. This prints 03:30 +02:00.var $date = Core\Time\Date::at(2024, 3, 31);var $time = Core\Time\TimeOfDay::at(2, 30);echo Core\Time\DateTime::at($date, $time, 'Europe/Berlin')->format('HH:mm XXX'), "\n";Not a date: Core\Time\Date::at(): parameter 'day' for `2023-02` is invalid, must be in range `1..=28`03:30 +02:00One meeting in many time zones
<?nvs// Shows how to plan a meeting in one time zone and show it in the zone of each person who joins.
// The meeting is at 09:00 in New York on 14 May 2024.var $day = Core\Time\Date::at(2024, 5, 14);var $meeting = Core\Time\DateTime::at($day, Core\Time\TimeOfDay::at(9, 0), 'America/New_York');
// `toInstant` gives the moment itself, and `in` shows that moment in another zone.// Tokyo is 13 hours ahead of New York in May, so it prints 22:00 for Tokyo.array<string> $cities = ['America/New_York', 'Europe/London', 'Europe/Berlin', 'Asia/Tokyo'];foreach ($cities as string $city) { var $local = $meeting->toInstant()->in(Core\Time\Zone::of($city)); echo $city, ': ', $local->format('yyyy-MM-dd HH:mm'), "\n";}America/New_York: 2024-05-14 09:00Europe/London: 2024-05-14 14:00Europe/Berlin: 2024-05-14 15:00Asia/Tokyo: 2024-05-14 22:00