PHP | date_time_set() Function

The date_time_set() function is an inbuilt function in PHP which is used to sets the time. This function resets the current time of the DateTime object to a different time.

Syntax:

  • Procedural style:
    date_time_set( $object, $hour, $minute, $second, $microseconds )
  • Object oriented style:
    DateTime::setTime( $hour, $minute, $second, $microseconds )

Parameters: This function accepts five parameters as mentioned above and described below:



  • $object: It is a mandatory parameter which is used to specify the DateTime object which is returned by the date_create() function.
  • $hour: This parameter is used to set hour of time.
  • $minute: This parameter is used to set minute of time.
  • $second: This parameter is used to set second of time.
  • $microsecond: This parameter is used to set microsecond of time.

Return Value: This function returns the DateTime object on success or False on failure.

Below programs illustrate the date_time_set() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create an DateTime object
$date = date_create('2018-09-15');
  
// Set the new DateTime
date_time_set($date, 8, 30);
  
// Display the date in given format
echo date_format($date, 'd-m-Y H:i:s') . "\n";
  
// Set the new DateTime
date_time_set($date, 12, 40, 30);
  
// Display the date in given format
echo date_format($date, 'Y-m-d H:i:s') . "\n";
?>

chevron_right


Output:

15-09-2018 08:30:00
2018-09-15 12:40:30

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create DateTime object
$date = new DateTime('2018-09-15');
  
// Set the new DateTime
$date->setTime(12, 30);
  
// Display the date in given format
echo $date->format('d-m-Y H:i:s') . "\n";
  
// Set the new DateTime
$date->setTime(12, 30, 20);
  
// Display the date in given format
echo $date->format('Y-m-d H:i:s');
?>

chevron_right


Output:

15-09-2018 12:30:00
2018-09-15 12:30:20

Related Articles:

Reference: http://php.net/manual/en/datetime.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.