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
$datetimeImmutable = new DateTimeImmutable( '2019-10-04' );
$Hour = '04' ;
$Minute = '10' ;
$Second = '40' ;
$a = $datetimeImmutable ->setTime( $Hour , $Minute , $Second );
echo $a ->format( 'Y-m-d H:i:s' );
?>
|
Output:
2019-10-04 04:10:40
Program 2:
php
<?php
$datetimeImmutable = new DateTimeImmutable( '2019-10-04' );
$a = $datetimeImmutable ->setTime(10, 33, 39);
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
Share your thoughts in the comments
Please Login to comment...