PHP date_date_set() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

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);
CallWhat 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

ParameterDescription
objectRequired. A DateTime object created with date_create() or new DateTime(). It is modified.
yearRequired. The new year as an integer.
monthRequired. The new month, normally 1 to 12. Values outside that range roll into the previous or next year.
dayRequired. The new day of the month, normally 1 to 31. Values outside the month roll into the previous or next month.

Return Value

Return ValueDescription
DateTimeThe 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().

PHP
<?php
$d = date_create('2024-01-15 10:30:00');

date_date_set($d, 2025, 6, 20);
echo date_format($d, 'Y-m-d H:i:s'), PHP_EOL;

$d->setDate(2026, 1, 1);
echo date_format($d, 'Y-m-d H:i:s'), PHP_EOL;

echo date_format(date_date_set($d, 2024, 12, 25), 'l'), PHP_EOL;

How It Works

  1. The object starts as January 15, 2024 at 10:30:00.
  2. date_date_set() replaces the date with 2025-06-20, and the time stays 10:30:00.
  3. $d->setDate() is the same function in object form.
  4. Because the function returns the object, it can be passed straight into date_format(). December 25, 2024 is a Wednesday.

Example 2 — Values That Roll Over

PHP does not reject impossible values. It carries them into the next or previous month or year.

PHP
<?php
$d = date_create('2024-01-15');

date_date_set($d, 2024, 13, 1);
echo date_format($d, 'Y-m-d'), PHP_EOL;

date_date_set($d, 2024, 3, 0);
echo date_format($d, 'Y-m-d'), PHP_EOL;

date_date_set($d, 2023, 2, 30);
echo date_format($d, 'Y-m-d'), PHP_EOL;

How It Works

  1. Month 13 means the month after December, so the result is January 1, 2025.
  2. Day 0 means the day before the first of March, which is February 29 in the leap year 2024.
  3. February 2023 has only 28 days, so day 30 moves forward by two days into March.
  4. None of these calls produce an error, so validate input yourself when needed.

Example 3 — Validate First and Use Immutable Dates

Guard the call with checkdate(). For immutable objects use setDate(), which returns a new object.

PHP
<?php
$d = date_create('2024-01-15');
$year = 2023;
$month = 2;
$day = 30;

if (checkdate($month, $day, $year)) {
    date_date_set($d, $year, $month, $day);
} else {
    echo "Invalid date: 2023-02-30", PHP_EOL;
}
echo date_format($d, 'Y-m-d'), PHP_EOL;

$im = new DateTimeImmutable('2024-01-15');
$new = $im->setDate(2030, 5, 5);
echo $im->format('Y-m-d'), ' -> ', $new->format('Y-m-d'), PHP_EOL;

How It Works

  1. checkdate(2, 30, 2023) returns false, so date_date_set() is skipped.
  2. The error message is printed and the original date stays 2024-01-15.
  3. For a DateTimeImmutable, setDate() returns a new object.
  4. 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.

Next: date_default_timezone_get()

date_default_timezone_get() returns the timezone identifier that PHP currently uses as its.

date_default_timezone_get() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful