In PHP, date_date_set() changes the year, month and day of a DateTime object while keeping its time of day. It changes the object and returns it.
date_date_set() is the procedural alias of DateTime::setDate(). It is handy when you want to move a date to a new calendar day but keep the hour, minute and second. Values that are out of range are not rejected: PHP rolls them over, so month 13 becomes January of the next year.
Syntax
Call date_date_set() with the values you need. Start with this line.
PHP
date_date_set($date, 2025, 6, 20);
Call
What it does
date_date_set($date, 2025, 6, 20)
Changes the date to June 20, 2025 and keeps the time.
date_date_set($date, 2024, 13, 1)
Month 13 rolls over to January 1 of the next year.
$date->setDate(2026, 1, 1)
The object style (DateTime::setDate) that does the same thing as date_date_set().
date_date_set() is an alias of DateTime::setDate() and only accepts a DateTime. For DateTimeImmutable, call $immutable->setDate(), which returns a new object. Check the values with checkdate() first if you do not want automatic rollover.
Library declarations
PHP
date_date_set(DateTime $object, int $year, int $month, int $day): DateTime
Parameters
Parameter
Description
object
Required. A DateTime object created with date_create() or new DateTime(). It is modified.
year
Required. The new year as an integer.
month
Required. The new month, normally 1 to 12. Values outside that range roll into the previous or next year.
day
Required. The new day of the month, normally 1 to 31. Values outside the month roll into the previous or next month.
Return Value
Return Value
Description
DateTime
The same DateTime object that was passed in, now with the new date. Because it is the same object, the original variable already holds the new value.
PHP Examples
Example 1 — Changing the Date but Keeping the Time
Set a new year, month and day. The time stays at 10:30:00. The function also returns the object so you can chain it into date_format().
checkdate(2, 30, 2023) returns false, so date_date_set() is skipped.
The error message is printed and the original date stays 2024-01-15.
For a DateTimeImmutable, setDate() returns a new object.
The original immutable keeps 2024-01-15 and the new object holds 2030-05-05.
Frequently Asked Questions
It sets a new year, month and day on a DateTime object and leaves the time of day alone. It changes the object in place and returns it.
Yes. date_date_set() is the procedural alias of DateTime::setDate(). Use whichever style matches your code.
No. Only the year, month and day change. The hours, minutes, seconds and timezone stay the same.
PHP does not throw an error. It rolls the extra days into the next month, so February 30, 2023 becomes March 2, 2023. Use checkdate() first to reject invalid input.
Call $immutable->setDate($year, $month, $day) and store the returned object. date_date_set() only accepts DateTime, not DateTimeImmutable.