setTime() sets hour, minute, second and optionally microseconds while preserving the date and timezone.
$date = new DateTimeImmutable('2026-09-14');
$changed = $date->setTime(14, 30, 15);
echo $changed->format('Y-m-d H:i:s');Hours and minutes are interpreted in the object's timezone.
setTime( Hour, minutes, seconds )
All above inputs are in integer. date_default_timezone_set('America/Chicago');
$date = new DateTime('2012-04-15 22:15:40');
echo $date->format('Y/m/d H:i:s') . "<br>";
$date->setTime(23,30);
echo $date->format('Y/m/d H:i:s') . "<br>";
The output is here 2012/04/15 22:15:40
2012/04/15 23:30:00We can change the setTime to different values and output will change like this . $date->setTime(2,50,32);output is here 2012/04/15 22:15:40
2012/04/15 02:50:32
$date = date_create('2020-08-16');
date_time_set($date, 24, 56,58);
echo date_format($date, 'Y-m-d H:i:s');
2020-08-17 00:56:58
In the above code check how the hour has changed the day by adding one.
$date = new DateTime();
$date->setTime(14,30,45, 500000); //2:30:45 PM and 500000 microseconds
echo $date->format('Y-m-d H:i:s.u'); // Output: 14:30:45.500000
Output
2026-10-20 14:30:45.500000
$date = new DateTime(); // Current date and time
$currentHour = (int) $date->format('H');
$currentMinute = (int) $date->format('i');
$currentSecond = (int) $date->format('s');
$currentMicrosecond = (int) $date->format('u');
$date->setTime($currentHour, $currentMinute, $currentSecond, $currentMicrosecond);
echo $date->format('Y-m-d H:i:s.u'); // Output includes current date with precise time
Output ( refresh this page to see the changes )
2026-10-20 01:40:20.887445
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.