The date_time_set() function in PHP allows you to modify the time portion of an existing DateTime object by setting hours, minutes, seconds, and optional microseconds.
date_time_set() works only with DateTime objectsThis function directly updates the internal time of a DateTime instance.
Syntax:
date_time_set(DateTime $object, int $hour, int $minute, int $second = 0, int $microsecond = 0)
// Create a DateTime object and update its time
<?php
$datetime = new DateTime("2025-07-18 12:30:00");
// Setting time to 15:45:00
date_time_set($datetime, 15, 45, 0);
echo $datetime->format('Y-m-d H:i:s');
?>
The original time 12:30:00 is updated to 15:45:00 while keeping the date unchanged (2025-07-18). The formatted output confirms the updated time.
Think of DateTime as a clock attached to a calendar. The date_time_set() function adjusts only the clock hands without touching the calendar date.
DateTime object before modificationformat() to confirm changes visuallyDateTime (OOP style) over legacy procedural date functionsDateTime object for today and set time to 08:00:00date() valuesdate_time_set()