In PHP, date_time_set() changes the hour, minute, second, and microsecond of a DateTime object while keeping the date.
It is the procedural name for DateTime::setTime() (and DateTimeImmutable::setTime()). The DateTime object is changed in place and also returned.
Syntax
Call date_time_set() with the values you need. Start with this line.
PHP
date_time_set($date, 14, 30);
Call
What it does
date_time_set($date, 14, 30)
Sets the time to 14:30:00.
date_time_set($date, 9, 5, 45)
Sets the time to 09:05:45.
date_time_set($date, 0, 0)
Moves the object to midnight at the start of the same day.
The seconds and microseconds reset to 0 if you leave them out, so date_time_set($date, 14, 30) always gives exactly 14:30:00. In new code, DateTimeImmutable::setTime() lets you set a time without changing the original object.
Library declarations
PHP
date_time_set(DateTime $object, int $hour, int $minute, int $second = 0, int $microsecond = 0): DateTime
Parameters
Parameter
Description
object
Required. The DateTime object to change.
hour
Required. The hour, usually 0 to 23.
minute
Required. The minute, usually 0 to 59.
second
Optional. The second, usually 0 to 59. The default is 0.
microsecond
Optional. The microsecond. The default is 0.
Return Value
Return Value
Description
DateTime
The same DateTime object after the time has been set.
PHP Examples
Example 1 — Change the Time of Day
Set a new time on an existing date. The date does not change.