Core\Time\DateTime::with
$dateTime->with({…}): Core\Time\DateTime$dateTime->with( {…}, ): Core\Time\DateTime
Returns a new date and time with some of its parts replaced.
You name only the parts you want to change: year, month, day, hour, minute, second or nanoseconds. The other parts and the time zone stay as they are. The value you call with on does not change.
The new parts must make a date that exists. 14 June with the day changed to 31 is not in the calendar, so with throws a RuntimeError. plus is different: it gives the last day of the month in that case.
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. In Berlin on 31 March 2024, a change to 02:30 gives 03:30.
Options
yearintoptionalThe new year, from-9999to9999. Leave it out to keep the year.monthuintoptionalThe new month, from1to12. Leave it out to keep the month.dayuintoptionalThe new day of the month. Leave it out to keep the day.houruintoptionalThe new hour, from0to23. Leave it out to keep the hour.minuteuintoptionalThe new minute, from0to59. Leave it out to keep the minute.seconduintoptionalThe new second, from0to59. Leave it out to keep the second.nanosecondsuintoptionalThe new part of a second, in nanoseconds, from0to999999999. Leave it out to keep it.
Return value
Returns Core\Time\DateTime. A new DateTime in the same zone. The value you call with on does not change.
Errors
RuntimeErrorA value is outside its range. Or the parts together are not a date that exists, such as{ day: 31 }on a date in June.
Examples
Change some fields
<?nvs// Shows how to change some fields of a date and time and keep the others.
var $meeting = Core\Time\DateTime::fromIso('2024-06-14 16:45', 'Europe/Berlin');
// `with` returns a new value. Each option you give replaces one field.// This prints 2024-06-01 16:45.echo $meeting->with({ day: 1 })->format('yyyy-MM-dd HH:mm'), "\n";
// This prints 2024-06-14 09:00.echo $meeting->with({ hour: 9, minute: 0 })->format('yyyy-MM-dd HH:mm'), "\n";
// This prints 2025-02-28 16:45.echo $meeting->with({ year: 2025, month: 2, day: 28 })->format('yyyy-MM-dd HH:mm'), "\n";
// `$meeting` itself does not change. This prints 2024-06-14 16:45.echo $meeting->format('yyyy-MM-dd HH:mm'), "\n";2024-06-01 16:452024-06-14 09:002025-02-28 16:452024-06-14 16:45Dates that do not exist
<?nvs// Shows what `with` does with a date that does not exist and with a time the clocks skip.
var $berlin = Core\Time\Zone::of('Europe/Berlin');var $june = Core\Time\DateTime::fromIso('2024-06-14', $berlin);
// June has only 30 days, so `{ day: 31 }` throws a `RuntimeError`.// This prints the message of the error.try { $june->with({ day: 31 });} catch (RuntimeError $error) { echo $error->message, "\n";}
// On 31 March 2024, Berlin moves its clocks from 02:00 to 03:00. That day has no 02:30.// `with` moves the time forward by the missing hour. This prints 2024-03-31 03:30 +02:00.var $spring = Core\Time\DateTime::fromIso('2024-03-31 12:00', $berlin);echo $spring->with({ hour: 2, minute: 30 })->format('yyyy-MM-dd HH:mm XXX'), "\n";Core\Time\DateTime::with(): parameter 'day' for `2024-06` is invalid, must be in range `1..=30`2024-03-31 03:30 +02:00When an order is delivered
<?nvs// Shows how a shop works out when an order is delivered.
// An order placed before 14:00 is delivered the next day. A later order takes one day more.// Every delivery arrives at 10:00.class Delivery{ public static function arrives(Core\Time\DateTime $ordered): Core\Time\DateTime { var $cutoff = $ordered->with({ hour: 14, minute: 0, second: 0, nanoseconds: 0 }); int $days = 1; if ($ordered->difference($cutoff, Core\Unit::Second) <= 0) { $days = 2; } return $ordered->plus($days, Core\Unit::Day)->with({ hour: 10, minute: 0, second: 0, nanoseconds: 0 }); }}
var $vienna = Core\Time\Zone::of('Europe/Vienna');
// Both orders are placed on Monday 13 May 2024.// This prints Tuesday 14 May, 10:00 and Wednesday 15 May, 10:00.var $morning = Core\Time\DateTime::fromIso('2024-05-13 09:40', $vienna);var $afternoon = Core\Time\DateTime::fromIso('2024-05-13 16:05', $vienna);echo Delivery::arrives($morning)->format('EEEE d MMMM, HH:mm'), "\n";echo Delivery::arrives($afternoon)->format('EEEE d MMMM, HH:mm'), "\n";Tuesday 14 May, 10:00Wednesday 15 May, 10:00