PHP Tutorial - 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

ParameterIs RequiredDescription
objectRequired.DateTime object returned by date_create()
hourRequired.hour of the time
minuteRequired.minute of the time
secondOptional.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
$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
$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//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.