php8.5
Home/ Manual/ datetime / datetimeimmutable/ DateTimeImmutable::setDate

DateTimeImmutable::setDate

PHP function Edit on GitHub ✎

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

Sets the date

Description

DateTimeImmutable::setDate(int $year, int $month, int $day): DateTimeImmutable

Returns a new DateTimeImmutable object with the current date of the DateTimeImmutable object set to the given date.

Parameters

year

Year of the date.

month

Month of the date.

day

Day of the date.

Return Values

Modifiedobject

Examples

DateTimeImmutable::setDate() example

Oop

php
<?php
$date = new DateTimeImmutable();
$newDate = $date->setDate(2001, 2, 3);
echo $newDate->format('Y-m-d');

The above example will output:

output
2001-02-03

Values exceeding ranges are added to their parent values

php
<?php
$date = new DateTimeImmutable();

$newDate = $date->setDate(2001, 2, 28);
echo $newDate->format('Y-m-d') . "\n";

$newDate = $date->setDate(2001, 2, 29);
echo $newDate->format('Y-m-d') . "\n";

$newDate = $date->setDate(2001, 14, 3);
echo $newDate->format('Y-m-d') . "\n";

The above example will output:

output
2001-02-28
2001-03-01
2002-02-03

See Also

Source: reference/datetime/datetimeimmutable/setdate.xml · from the official PHP manual (php/doc-en)