Open In App

PHP | DateTimeImmutable setDate() Function

Last Updated : 11 Oct, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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

// 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

// 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

Next Article

Similar Reads