English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and examples of PHP date_date_set() function

PHP Data & Time Function Manual

The date_date_set() function sets the date of the DateTime object

Definition and usage

The date_date_set() function is an alias for DateTime::setDate(). Using this function, you can (re-)set the date of the DateTime object.

Syntax

date_date_set($object, $year, $month, $day)

Parameter

Serial numberParameters and descriptions
1

object(必需)

This is a DateTime object, you need to set the date for it.

2

year(必需)

Year

3

month(必需)

Month

4

day(必需)

Day.

Return value

 Returns the modified DateTime object; if the function fails, it will return the boolean value false.

PHP version

This function was initially introduced in PHP version 5.2.0 and can be used in all higher versions.

Online Example

The following examples demonstratedate_date_setUsage of the function-

<?php
   // Create date
   $date = new DateTime();
   // Set date
   date_date_set($date, 2019, 07, 17);   
   print("Date: ". date_format($date, "Y/m/d"));
?>
Test and see </>

Output result

Data: 17/07/2019

Online Example

The following example creates a DateTime object and usesdate_date_set()The function modifies its date.-

<?php
   //Data string
   $date_string = "25-09-1989";
   // Create a DateTime object
   $date_time_Obj = date_create($date_string);
   print("Original Date: ". date_format($date_time_Obj, "Y/m/d"));
   print("\n");
   // Set date
   $date = date_date_set($date_time_Obj, 2015, 11, 25);   
   print("Modified Date: ". date_format($date, "Y/m/d"));
?>
Test and see </>

Output result

Original Date: 1989/09/25
Modified Date: 2015/11/25

Online Example

When calling this function, if the day and month values you pass are out of range, they will be added to the parent values-

<?php
   // Create date
   $date = new DateTime();
   // Set date
   date_date_set($date, 2019, 15, 17);   
   print("Date: ". date_format($date, "Y/m/d"));
?>
Test and see </>

Since we set the month value to 15, three months are added to the appropriate date-

Date: 2020/03/17

Online Example

Set a new date using date_date_set()

<?php
$dateSrc = '2005-04-19 12:50 GMT';
$dateTime = date_create($dateSrc);;
# Now use date_date_set() to set a new date;
date_date_set($dateTime, 2000, 12, 12);
   
echo "The new formatted date is ". $dateTime->format("Y-m-d\TH:i:s\Z");
echo "
";
# Use the second function.
$dateTime = new DateTime($dateSrc);
$dateTime->setDate(1999, 10, 12);
   
echo "The new formatted date is ". $dateTime->format("Y-m-d\TH:i:s\Z");
?>
Test and see </>

Output result:

The new formatted date is 2000-12-12T12:50:00Z
The new formatted date is 1999-10-12T12:50:00Z

PHP Data & Time Function Manual