Friday, October 24, 2025
HomeLanguagesPHP | DateTimeImmutable setDate() Function

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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS