PHP | DateTime setDate() Function
The DateTime::setDate() function is an inbuilt function in PHP which is used to reset the current date of DateTime object with the given date-time object.
Syntax:
- Object oriented style:
DateTime DateTime::setDate( int $year, int $month, int $day )
- Procedural style:
DateTime date_date_set( DateTime $object, int $year, int $month, int $day )
Parameters: This function accepts three parameters as mentioned above and described below:
- $year: This parameter holds the value of year of the date.
- $month: This parameter holds the value of month of the date.
- $day: This parameter holds the value of day of the date.
Return Value: This function returns a new DateTime object on success or False on failure.
Below programs illustrate the DateTime::setDate() function in PHP:
Program 1 :
<?php // PHP program to illustrate // DateTime::setDate() function // Creating a new DateTime() object $datetime = new DateTime(); // Initialising year, month and day $Year = '2019'; $Month = '09'; $Day = '30'; // Calling the setDate() function $datetime->setDate($Year, $Month, $Day); // Getting a new set of date in the // format of 'Y-m-d' echo $datetime->format('Y-m-d'); ?> |
2019-09-30
Program 2:
<?php // PHP program to illustrate // DateTime::setDate() function // Creating a new DateTime() object $datetime = new DateTime(); // Calling the setDate() function // with parameters like years of 2019, // month of 10 and day of 1 $datetime->setDate(2019, 10, 01); // Getting a new set of date in the // format of 'Y-m-d' echo $datetime->format('Y-m-d'); ?> |
2019-10-01
Reference: https://www.php.net/manual/en/datetime.setdate.php
Recommended Posts:
- PHP | DateTimeImmutable setDate() Function
- PHP | DateTime sub() Function
- PHP | DateTime add() Function
- How to convert JavaScript datetime to MySQL datetime ?
- PHP | DateTime format() Function
- PHP | DateTime diff() Function
- PHP | DateTime modify() Function
- PHP | DateTime createFromFormat() Function
- HTML | datetime Attribute
- HTML | DOM del dateTime Property
- HTML | <ins> datetime Attribute
- HTML | DOM ins dateTime Property
- HTML | <del> datetime Attribute
- PHP | Converting string to Date and DateTime
- HTML | DOM Input Datetime max Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



