PHP | DateTimeImmutable setTime() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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');
?>

chevron_right


Output:

2019-10-04 04:10:40

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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');
?>

chevron_right


Output:

2019-10-04 10:33:39

Reference: https://www.php.net/manual/en/datetimeimmutable.settime.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.