The DateTimeImmutable::setTime() function is an inbuilt function in PHP which is used to sets the desired time by resetting the current time of the created DateTimeImmutable object.
Syntax:
DateTimeImmutable DateTimeImmutable::setTime( int $hour, int $minute,
int $second, int $microseconds )
Parameters: This function accepts four parameters as mentioned above and described below:
- $hour: This parameter is used to set the hou time.
- $minute: This parameter is used to set the minute time.
- $second: This parameter is used to set the second time.
- $microseconds: This parameter is used to set the microseconds time.
Return Values: This function returns a new date time object.
Below programs illustrate the DateTimeImmutable::setTime() function in PHP:
Program 1:
<?php // PHP program to illustrate DateTimeImmutable::setTime() // function // Creating a new DateTimeImmutable() object $datetimeImmutable = new DateTimeImmutable('2019-10-04'); // Initialising Hour, Minute and Second $Hour = '04'; $Minute = '10'; $Second = '40'; // Calling the DateTimeImmutable::setTime() function $a = $datetimeImmutable->setTime($Hour, $Minute, $Second); // Getting a new set of date and time in the // format of 'Y-m-d H:i:s' echo $a->format('Y-m-d H:i:s'); ?> |
2019-10-04 04:10:40
Program 2:
<?php // PHP program to illustrate DateTimeImmutable::setTime() // function // Creating a new DateTimeImmutable() object $datetimeImmutable = new DateTimeImmutable('2019-10-04'); // Calling the setTime() function // with parameters like Hour of 10, // minute of 33 and second of 39 $a = $datetimeImmutable->setTime(10, 33, 39); // Getting a new set of date and time in the // format of 'Y-m-d H:i:s' echo $a->format('Y-m-d H:i:s'); ?> |
2019-10-04 10:33:39
Reference: https://www.php.net/manual/en/datetimeimmutable.settime.php
Recommended Posts:
- PHP | IntlCalendar setTime() Function
- PHP | DateTimeImmutable::sub() Function
- PHP | DateTimeImmutable add() Function
- PHP | DateTimeImmutable setDate() Function
- PHP | DateTimeImmutable::setTimezone() Function
- PHP | DateTimeImmutable setISODate() Function
- PHP | DateTimeImmutable modify() Function
- PHP | DateTimeImmutable setTimestamp() Function
- How to call a function that return another function in JavaScript ?
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- PHP | each() Function
- CSS | hsl() Function
- D3.js now() function
- p5.js | log() function
- D3.js | d3.map.set() Function
- PHP | ord() Function
- p5.js | red() function
- p5.js | str() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

