The date_time_set() function sets the time.
PHP date_time_set() Function has the following syntax.
date_time_set(object,hour,minute,second);
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 |
PHP date_time_set() Function returns a DateTime object on success. FALSE on failure.
Object oriented style
<?php
$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.
Procedural style
<?php
$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.
Values exceeding ranges are added to their parent values
<?php//from www. j a v a 2 s . c o m
$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.