setDate(): sets the date in an existing DateTime object

Set year, month and day with setDate()

setDate() replaces the calendar date while preserving the time portion. DateTime mutates; DateTimeImmutable returns a new object.

$date = new DateTimeImmutable('2026-01-01 08:30:00');
$changed = $date->setDate(2026, 9, 14);
echo $changed->format('Y-m-d H:i:s');

Use the returned object when calling setDate() on DateTimeImmutable.

We can assign new date to any date object by using setDate function. Using this we can change the date object.
setDate(Year, month, day)
Here are some examples to set the date.
date_default_timezone_set('America/Chicago');
$today = new DateTime; echo $today->format('Y-m-d '); echo "<br>"; $today->setDate(13,11,20); echo $today->format('Y-m-d');
The output is here
2026-09-29 
0013-11-20
IN above code we can change the setDate function parameters and check the output. We can change to higher values for year and month , the function will adjust it to the next values and display the correct dates. Here are the changes ( only )
$today->setDate(13,14,31);
The output is here
2026-09-29 
0014-03-03

Procedural style

$date = date_create();
date_date_set($date, 2019, 8, 13);
echo date_format($date, 'Y-m-d');
Output is here
2019-08-13
PHP Date Functions setTime() : Set time for date object Timestamp in PHP Displaying calendar for selection of date by user


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer