PHP date_time_set() Function

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

Definition

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

ParameterDescription
objectRequired. The DateTime object to change.
hourRequired. The hour, usually 0 to 23.
minuteRequired. The minute, usually 0 to 59.
secondOptional. The second, usually 0 to 59. The default is 0.
microsecondOptional. The microsecond. The default is 0.

Return Value

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

PHP
<?php
$date = date_create("2024-03-15 08:45:30");

date_time_set($date, 14, 30);
echo date_format($date, "Y-m-d H:i:s"), PHP_EOL;

date_time_set($date, 9, 5, 45);
echo date_format($date, "Y-m-d H:i:s"), PHP_EOL;

How It Works

  1. The starting date has a time of 08:45:30.
  2. date_time_set() with 14 and 30 sets the time to 14:30, and the seconds reset to 0.
  3. A second call with 9, 5, and 45 replaces the time again.
  4. The date, March 15, stays the same each time.

Example 2 — Midnight and Overflowing Values

Use 0, 0 to go to midnight. Large values spill over into the next day.

PHP
<?php
$date = date_create("2024-03-15 08:45:30");

date_time_set($date, 0, 0);
echo date_format($date, "Y-m-d H:i:s"), PHP_EOL;

date_time_set($date, 25, 61, 61);
echo date_format($date, "Y-m-d H:i:s"), PHP_EOL;

How It Works

  1. date_time_set($date, 0, 0) moves the object to midnight on March 15.
  2. The values 25, 61, and 61 are out of range, but PHP does not throw an error.
  3. 25 hours, 61 minutes, and 61 seconds add up to 26 hours, 2 minutes, and 1 second.
  4. That is 1 day and 02:02:01 after the start of March 15, which is March 16 at 02:02:01.

Example 3 — Closing Time with DateTimeImmutable

Use setTime() on a DateTimeImmutable to build a new time without touching the original.

PHP
<?php
$now = new DateTimeImmutable("2024-03-15 10:20:00");
$closing = $now->setTime(17, 0);

echo "Now: ", $now->format("H:i"), PHP_EOL;
echo "Closing: ", $closing->format("H:i"), PHP_EOL;
echo "Closes in: ", $now->diff($closing)->format("%h hours %i minutes"), PHP_EOL;

How It Works

  1. Create the current time as a DateTimeImmutable.
  2. setTime(17, 0) returns a new object at 17:00 on the same day.
  3. The original $now is still 10:20.
  4. diff() compares the two times, and format() shows 6 hours 40 minutes.

Frequently Asked Questions

It sets the hour, minute, second, and microsecond of a DateTime object without changing its date. The object is changed and also returned.
Call date_time_set($date, 0, 0). Seconds and microseconds default to 0, so the time becomes 00:00:00 on the same day.
PHP does not give an error. The extra hours roll over into the next day, so an hour of 25 means 01:00 on the following day.
Yes, for DateTime it does. If you want to keep the original, use DateTimeImmutable::setTime(), which returns a new object.
Yes. It is the procedural alias of DateTime::setTime(), which also exists on DateTimeImmutable.

Next: date_timestamp_get()

date_timestamp_get() returns the Unix timestamp of a date object, which is the number of s.

date_timestamp_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