HomeLanguagesPHP | DateTimeImmutable setTime() Function

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 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
 

RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS