PHP | DateTime setDate() Function
Last Updated :
10 Oct, 2019
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
$datetime = new DateTime();
$Year = '2019' ;
$Month = '09' ;
$Day = '30' ;
$datetime ->setDate( $Year , $Month , $Day );
echo $datetime ->format( 'Y-m-d' );
?>
|
Program 2:
<?php
$datetime = new DateTime();
$datetime ->setDate(2019, 10, 01);
echo $datetime ->format( 'Y-m-d' );
?>
|
Reference: https://www.php.net/manual/en/datetime.setdate.php
Share your thoughts in the comments
Please Login to comment...