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/*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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions