Monday 26 August 2013

PHP Function date_time_set()

Syntax

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.

Paramters

ParameterDescription
objectRequired. DateTime object
hourRequired. Hour to be set
minuteRequired. Minute to be set
secondOptional. Second to be set

Return Value

Returns NULL on success or FALSE on failure.

Example

Following is the usage of this function:
<?php
   $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 following result:
DateTime::format(): 2007:Apr:Thu:20:40:10
DateTime::format(): 2007:Apr:Thu:20:56:06

No comments:

Post a Comment