PHP Function date_time_set()

In PHP, the date_time_set() function is used to set a new time for a given date. This function is part of the PHP DateTime class and can be used to set the time portion of a date/time string or object to a new value.


The syntax for the date_time_set() function is as follows:

date_time_set($datetime, $hour, $minute, $second);


  • $datetime is the date/time object or string to modify.
  • $hour is the new hour value.
  • $minute is the new minute value.
  • $second is the new second value (optional, default is 0).

Example Usage

Let's take a look at an example of how to use the date_time_set() function:

$date = new DateTime('2000-01-01 12:00:00');
date_time_set($date, 14, 30);
echo $date->format('Y-m-d H:i:s');

In this example, we first create a new DateTime object with the date and time set to '2000-01-01 12:00:00'. We then use the date_time_set() function to set the hour to 14 and the minute to 30. Finally, we use the format() method to display the modified date and time in the format 'Y-m-d H:i:s'.

How date_time_set() Compares to Other Functions

The date_time_set() function is similar to the setTime() method of the DateTime class. However, date_time_set() allows for more flexible input by accepting both DateTime objects and date/time strings as input.


The date_time_set() function is a useful tool for manipulating dates and times in PHP. By allowing for flexible input, this function provides a convenient way to set the time portion of a date/time string or object to a new value.

Practice Your Knowledge

What does the date_default_timezone_set() function in PHP do?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?