Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_date_set() Function.
PHP date_date_set() function is an alias of the DateTime::setDate() function. By using this, you can reset the date of a DateTime object.
PHP date_date_set() function is an alias of the DateTime::setDate() function. By using this, you can reset the date of a DateTime object.
Syntax
Following below is the syntax to use this function -
date_date_set($object, $year, $month, $day)
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | object(Mandatory) This is a DateTime object to which you need to set the date. |
2 | year(Mandatory) This is an integer value representing the year of a date. |
3 | month(Mandatory) This is an integer value representing the month of a date. |
4 | day(Mandatory) This is an integer value representing the day of a date. |
Return Value
This PHP function returns the DateTime object with modified value. In the case of failure, it returns the boolean value false.
PHP Version
This function was first introduced as part of core PHP v 5.2.0 and, works with all the later versions.
Example1
The following example demonstrates the usage of the date_date_set() function -
<?php //Creating a date $date = new DateTime(); //Setting the date date_date_set($date, 2020, 11, 17); print("Date: ".date_format($date, "Y/m/d")); ?>
Output
When the above code is executed, it will produce the following result -
Date: 2020/11/17
Example2
The following example creates a DateTime object, and modifies its date using the PHP date_date_set() function -
<?php //Date string $date_string = "25-09-1989"; //Creating a DateTime object $date_time_Obj = date_create($date_string); print("Original Date: ".date_format($date_time_Obj, "Y/m/d")); print("\n"); //Setting the date $date = date_date_set($date_time_Obj, 2020, 11, 17 ); print("Modified Date: ".date_format($date, "Y/m/d")); ?>
Output
When the above code is executed, it will produce the following result -
Original Date: 1989/09/25 Modified Date: 2020/11/17
Example3
While invoking this function, if you pass the day and month value exceeding their range, they will be added to their parent values -
<?php //Creating a date $date = new DateTime(); //Setting the date date_date_set($date, 2019, 15, 17); print("Date: ".date_format($date, "Y/m/d")); ?>
Output
When the above code is executed, it will produce the following result -
Date: 2020/03/17
Example4
Try the following example below -
$dateSrc = '2005-04-19 12:50 GMT'; $dateTime = date_create( $dateSrc);; # Now set a new date using date_date_set(); date_date_set( $dateTime, 2000, 12, 12); echo "New Formatted date is ". $dateTime->format("Y-m-d\TH:i:s\Z"); echo "
"; # Using second function. $dateTime = new DateTime($dateSrc); $dateTime->setDate( 1999, 10, 12); echo "New Formatted date is ". $dateTime->format("Y-m-d\TH:i:s\Z");
Output
When the above code is executed, it will produce the following result -
New Formatted date is 2000-12-12T12:50:00Z New Formatted date is 1999-10-12T12:50:00Z
Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP date_default_timezone_get() Function.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.