PHP | DateTimeImmutable setDate() Function

The DateTimeImmutable::setDate() function is an inbuilt function in PHP which is used to set a new date in the created DateTimeImmutable object.
Syntax:
DateTimeImmutable DateTimeImmutable::setDate( int $year, int $month, int $day )
Parameters: This function accepts three parameters as mentioned above and described below:
- $year: This parameter holds the year value in integer format.
- $month: This parameter holds the month value in integer format.
- $day: This parameter holds the date value in integer format.
Return Values: This function returns the new date object.
Below programs illustrate the DateTimeImmutable::setDate() function in PHP:
Program 1:
<?php // PHP program to illustrate DateTimeImmutable::setDate() // function // Creating a new DateTimeImmutable() object $datetimeImmutable = new DateTimeImmutable(); // Initialising year, month and day $Year = '2019'; $Month = '10'; $Day = '03'; // Calling the DateTimeImmutable::setDate() function $a = $datetimeImmutable->setDate($Year, $Month, $Day); // Getting a new set of date in the // format of 'Y-m-d' echo $a->format('Y-m-d'); ?> |
Output:
2019-10-03
Program 2:
<?php // PHP program to illustrate DateTimeImmutable::setDate() // function // Creating a new DateTimeImmutable() object $datetimeImmutable = new DateTimeImmutable(); // Calling the setDate() function // with parameters like years of 2019, // month of 10 and day of 3 $a = $datetimeImmutable->setDate(2019, 10, 03); // Getting a new set of date in the // format of 'Y-m-d' echo $a->format('Y-m-d'); ?> |
Output:
2019-10-03
Reference: https://www.php.net/manual/en/datetimeimmutable.setdate.php



