Skip to content

Core\Time\Date::with

$date->with({year?: int, month?: uint, day?: uint}): Core\Time\Date$date->with(
    {year?: int, month?: uint, day?: uint},
): Core\Time\Date

Returns a new date with the year, the month or the day replaced.

You name only the parts you want to change, and the other parts stay as they are. The date you call with on does not change.

The new parts must make a date that exists. 29 February 2024 with the year changed to 2023 is not in the calendar, so with throws a RuntimeError. plus is different: it gives the last day of the month in that case.

A common use is the first day of a month: replace the day with 1. One month later, minus one day, is the last day of the same month.

Options

  • yearintoptionalThe new year, within -9999..=9999; omitted leaves the field alone.
  • monthuintoptionalThe new month, 1 to 12; omitted leaves the field alone.
  • dayuintoptionalThe new day of the month; omitted leaves the field alone.

Return value

Returns Core\Time\Date. A new Date; the receiver is unchanged.

Errors

  • RuntimeErrorAn option is outside its field's range, or the fields together are not a date that exists.

Examples

Change one part

<?nvs
// Shows how to change the year, the month or the day of a date.
var $start = Core\Time\Date::at(2024, 3, 15);
// `with` returns a new date. The parts you do not name stay the same.
// This prints 2025-03-15.
echo $start->with({ year: 2025 })->format('yyyy-MM-dd'), "\n";
// You can change more than one part at a time. This prints 2024-12-01.
echo $start->with({ month: 12, day: 1 })->format('yyyy-MM-dd'), "\n";
// `with` does not change the date it is called on. This prints 2024-03-15.
echo $start->format('yyyy-MM-dd'), "\n";
Output
2025-03-15
2024-12-01
2024-03-15

A date that does not exist

<?nvs
// Shows what `with` does when the new parts do not make a real date.
var $leapDay = Core\Time\Date::at(2024, 2, 29);
// 2023 is not a leap year, so 29 February 2023 does not exist.
// `with` throws a `RuntimeError`. It does not pick a nearby day.
try {
$leapDay->with({ year: 2023 });
} catch (RuntimeError $error) {
echo 'Not a date: ', $error->message, "\n";
}
// 2028 is a leap year, so this works. This prints 2028-02-29.
echo $leapDay->with({ year: 2028 })->format('yyyy-MM-dd'), "\n";
Output
Not a date: Core\Time\Date::with(): parameter 'day' for `2023-02` is invalid, must be in range `1..=28`
2028-02-29

The first and last day of a month

<?nvs
// Shows how to find the first and the last day of the month a date is in, for example for a monthly invoice.
array<Core\Time\Date> $orders = [
Core\Time\Date::at(2024, 2, 17),
Core\Time\Date::at(2024, 4, 3),
Core\Time\Date::at(2023, 12, 31),
];
// `with({ day: 1 })` gives the first day of the month. One month later, minus one day, is the last day.
// For the first order, this prints 2024-02-01 to 2024-02-29.
foreach ($orders as Core\Time\Date $ordered) {
var $first = $ordered->with({ day: 1 });
var $last = $first->plus(1, Core\Unit::Month)->minus(1, Core\Unit::Day);
echo $first->format('yyyy-MM-dd'), ' to ', $last->format('yyyy-MM-dd'), "\n";
}
Output
2024-02-01 to 2024-02-29
2024-04-01 to 2024-04-30
2023-12-01 to 2023-12-31