PHP - Function date_time_set()



void date_time_set ( DateTime $object, int $hour, int $minute [, int $second] )

void DateTime::setTime ( int $hour, int $minute [, int $second] )

Definition and Usage

These functions set the time in the passed object.

The above two functions are equivalent and any of the functions can be used as shown below in the example.


Parameter Description
object Required. DateTime object
hour Required. Hour to be set
minute Required. Minute to be set
second Optional. Second to be set

Return Value

Returns NULL on success or FALSE on failure.


Following is the usage of this function −

   $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 "\n";

   # Using second function.
   $dateTime = new DateTime($dateSrc);
   $retval = $dateTime->setTime(20, 56,6);
   echo 'DateTime::format(): '.$dateTime->format('Y:M:D:H:i:s');

This will produce the following result −

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