Open In App

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:



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

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:




<?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";
?>

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

Program 2:




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

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

Related Articles:

Reference: http://php.net/manual/en/datetime.settime.php


Article Tags :