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
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS