Core\Time\DateTime::withTime
$dateTime->withTime(Core\Time\TimeOfDay $t): Core\Time\DateTime$dateTime->withTime( Core\Time\TimeOfDay $t, ): Core\Time\DateTime
Returns a new date and time with the time of day replaced and the date kept.
You give a Core\Time\TimeOfDay. The result has its hour, minute, second and nanoseconds. The date and the time zone stay the same, and the value you call withTime on does not change.
Use it when you know the day and want a fixed time on that day, such as 08:00 for a reminder. with can change the same parts, but there you name each one. withTime always replaces all four, so the result has no seconds from the old time.
Good to know: when the clocks move forward, one hour of that day does not exist. If the new time is in that hour, the result is one hour later.
Parameters
$tCore\Time\TimeOfDayThe time of day to set, hour through nanosecond.
Return value
Returns Core\Time\DateTime. A new DateTime on the same date in the same zone; the receiver is unchanged.
Errors
RuntimeErrorThe result lies outside the representable range.
Examples
Set the time of day
<?nvs// Shows how to set the time of a date and time and keep its date and time zone.
var $due = Core\Time\DateTime::fromIso('2024-11-05 15:20', 'America/New_York');
// `withTime` returns a new value with the time from a `Core\Time\TimeOfDay`.// This prints 2024-11-05 09:00:00.echo $due->withTime(Core\Time\TimeOfDay::at(9, 0))->format('yyyy-MM-dd HH:mm:ss'), "\n";
// All four clock fields are replaced, also the seconds and the nanoseconds.// This prints 2024-11-05 23:59:59.999.var $late = Core\Time\TimeOfDay::at(23, 59, { second: 59, nanoseconds: 999000000 });echo $due->withTime($late)->format('yyyy-MM-dd HH:mm:ss.SSS'), "\n";
// The time zone does not change. This prints America/New_York.echo $due->withTime($late)->format('VV'), "\n";2024-11-05 09:00:002024-11-05 23:59:59.999America/New_YorkCopy the time to another day
<?nvs// Shows how to copy the time of day to another date, and what happens when the clocks change.
var $berlin = Core\Time\Zone::of('Europe/Berlin');var $firstRun = Core\Time\DateTime::fromIso('2024-03-01 02:30', $berlin);
// `timeOfDay` gives the clock time of `$firstRun`. This prints 2024-03-15 02:30.var $later = Core\Time\DateTime::fromIso('2024-03-15', $berlin);echo $later->withTime($firstRun->timeOfDay())->format('yyyy-MM-dd HH:mm'), "\n";
// On 31 March 2024, Berlin moves its clocks from 02:00 to 03:00. That day has no 02:30.// `withTime` moves the time forward by the missing hour. This prints 2024-03-31 03:30.var $spring = Core\Time\DateTime::fromIso('2024-03-31', $berlin);echo $spring->withTime($firstRun->timeOfDay())->format('yyyy-MM-dd HH:mm'), "\n";2024-03-15 02:302024-03-31 03:30A reminder in each time zone
<?nvs// Shows how to send a reminder at 08:00 on the day a task is due, in each user's own time zone.
array<string> $zones = ['Europe/London', 'Asia/Tokyo', 'America/Los_Angeles'];var $eight = Core\Time\TimeOfDay::at(8, 0);
// The deadline is one moment, but the date is not the same everywhere.var $deadline = Core\Time\Instant::fromIso('2024-09-20T23:30:00Z');
// `withTime` keeps the user's date and sets the time to 08:00.// This prints 2024-09-21 08:00 +01:00, 2024-09-21 08:00 +09:00 and 2024-09-20 08:00 -07:00.foreach ($zones as string $zone) { var $local = $deadline->in(Core\Time\Zone::of($zone)); echo $zone, ': ', $local->withTime($eight)->format('yyyy-MM-dd HH:mm XXX'), "\n";}Europe/London: 2024-09-21 08:00 +01:00Asia/Tokyo: 2024-09-21 08:00 +09:00America/Los_Angeles: 2024-09-20 08:00 -07:00