English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

Usage and examples of PHP date_time_set() function

PHP Date & Time Function Manual

The date_time_set() function sets the time of the DateTime object

Definition and usage

defineddate_time_set()The function isDateTime::setTime()Alias of the function. You can (re-)set the time of the DateTime object using this function.

Syntax

date_time_set($object, $hours, $minutes, $seconds, $microseconds)

Parameter

Serial numberParameters and descriptions
1

object (required)

This is a DateTime object for which you need to set the date.

2

hours (required)

This is an integer value representing the time to be set.

3

minute (required)

This is an integer value representing the minutes of the time to be set.

4

seconds (optional)

This is an integer value representing the seconds of the time to be set.

5

microseconds (optional)

This is an integer value representing the microseconds of the time to be set.

Return value

The date_time_set() function returns a DateTime object with the modified (time) value. If it fails, this function will return a boolean value.false.

PHP version

This function was originally introduced in PHP version5.2introduced in version 5.2.0 and can be used in all higher versions.

Online example

The following examples demonstratedate_time_setUsage of the function-

<?php
   //Create date
   $date = new DateTime();
   //Set date
   date_time_set($date, 7, 20, 35);   
   print("Date: " . date_format($date, "Y/m/d H:i:s"));
?>
Test to see‹/›

Output result

Date: 2020/05/10 07:20:35

Online example

The following example creates a DateTime object and usesdate_time_set()The function modifies its t .-

<?php
   //Date string
   $date_string = "25-09-1989 10:42:12";
   //Create a DateTime object
   $date_time_Obj = date_create($date_string);
   print("Original date: " . date_format($date_time_Obj, "Y/m/d H:i:s"));
   print("\n");
   //Set date
   $date = date_time_set($date_time_Obj, 6, 36, 3 );   
   print("Modify date: " . date_format($date, "Y/m/d H:i:s"));
?>
Test to see‹/›

Output result

Original date: 1989/09/25 10:42:12
Modify date: 1989/09/25 06:36:03

Online example

When calling this function, if the day and month values you pass exceed their range, they will be added to their parent value-

<?php
   //Create date
   $date = new DateTime();
   //Set date
   date_time_set($date, 24, 15, 36);   
   print("Date: " . date_format($date, "Y/m/d H:i:s"));
?>
Test to see‹/›

Because we set the month value to15. Three months are added to the appropriate time-

Date: 2020/05/11 00:15:36

Online example

$dateSrc = '2007-04-19 12:50 GMT';
$dateTime = date_create($dateSrc);;
$retval = date_time_set($dateTime, 20, 40, 10);
   
echo 'DateTime::format(): ' . $dateTime->format('Y:M:D:H:i:s');
echo '
';
# Use the second function.
$dateTime = new DateTime($dateSrc);
$retval = $dateTime->setTime(20, 56,6);
   
echo 'DateTime::format(): ' . $dateTime->format('Y:M:D:H:i:s');
Test to see‹/›

Output result:

DateTime::format(): 2007:Apr:Thu:20:40:10
DateTime::format(): 2007:Apr:Thu:20:56:06