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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP date_date_set() function usage and examples

PHP Date & Time Functions 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(). With this function, you can (re-)set the date of the DateTime object.

Syntax

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

Parameter

Serial NumberParameters and Description
1

object(Required)

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

2

year(Required)

Year

3

month(Required)

Month

4

day(Required)

Day.

Return value

 Returns the modified DateTime object. If the operation fails, this function will return the boolean value false.

PHP version

This function was originally introduced in PHP version5.2.0 was introduced in and can be used in all higher versions.

Online Example

The following examples demonstratedate_date_setfunction usage-

<?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

Date: 2019/07/17

Online Example

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

<?php
   //Date 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 exceed their range, they will be added to their parent value-

<?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‹/›

Because we set the month value to15. Three months are added to the appropriate date-

Date: 2020/03/17

Online Example

Use date_date_set() to set a new date

<?php
$dateSrc = '2005-04-19 12:50 GMT';
$dateTime = date_create($dateSrc);;
#Now use date_date_set() to set the 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 Date & Time Functions Manual