PHP date_time_set() Function
In this chapter you will learn:
- Definition for PHP date_time_set() Function
- Syntax for PHP date_time_set() Function
- Parameter for PHP date_time_set() Function
- Return for PHP date_time_set() Function
- Example - Set the time
- Example - Procedural style
- Example - Values exceeding ranges are added to their parent values
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 java2 s . c om
$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/* j ava2 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// java 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.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP date_timestamp_get() Function
- Syntax for PHP date_timestamp_get() Function
- Parameter for PHP date_timestamp_get() Function
- Return value for PHP date_timestamp_get() Function
- Example - Return the Unix timestamp for today's date and time
Home » PHP Tutorial » PHP Date Functions