PHP date_time_set() Function
Definition
The date_time_set() function sets the time.
Syntax
PHP date_time_set() Function has the following syntax.
date_time_set(object,hour,minute,second);
Parameter
Parameter | Is Required | Description |
---|---|---|
object | Required. | DateTime object returned by date_create() |
hour | Required. | hour of the time |
minute | Required. | minute of the time |
second | Optional. | second of the time. Default is 0 |
Return
PHP date_time_set() Function returns a DateTime object on success. FALSE on failure.
Example
Object oriented style
<?php/*from www . j a v a2 s.c o m*/
$date = new DateTime('2013-01-01');
$date->setTime(14, 55);
echo $date->format('Y-m-d H:i:s') . "\n";
$date->setTime(14, 55, 24);
echo $date->format('Y-m-d H:i:s') . "\n";
?>
The code above generates the following result.
Example 2
Procedural style
<?php/*from w w w. j av a 2 s .c o m*/
$date = date_create('2013-01-01');
date_time_set($date, 14, 55);
echo date_format($date, 'Y-m-d H:i:s') . "\n";
date_time_set($date, 14, 55, 24);
echo date_format($date, 'Y-m-d H:i:s') . "\n";
?>
The code above generates the following result.
Example 3
Values exceeding ranges are added to their parent values
<?php/* www. j a v a 2 s . c om*/
$date = new DateTime('2013-01-01');
$date->setTime(14, 52, 21);
echo $date->format('Y-m-d H:i:s') . "\n";
$date->setTime(14, 52, 61);
echo $date->format('Y-m-d H:i:s') . "\n";
$date->setTime(14, 62, 21);
echo $date->format('Y-m-d H:i:s') . "\n";
$date->setTime(25, 52, 21);
echo $date->format('Y-m-d H:i:s') . "\n";
?>
The code above generates the following result.