Open In App

PHP | DateTimeImmutable setTime() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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


Output: 

2019-10-04 04:10:40

 

Program 2: 
 

php




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


Output: 

2019-10-04 10:33:39

 

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



Last Updated : 11 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads