We now have a youtube channel. Subscribe!

PHP | date_time_set() Function

PHP date_time_set() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP date_time_set() Function.

PHP date_time_set() function is an alias of the DateTime::setTime() function. Making use of this function, you can reset the time of a DateTime object.

Syntax

Following below is the syntax to use this function -

date_time_set($object, $hours, $minutes, $seconds, $microseconds)


Parameter Details

Sr.NoParameter & Description
1

object(Mandatory)

This is a DateTime object to which you need to set the date.

2

hours(Mandatory)

This is an integer value representing the hour of the time to be set.

3

minute(Mandatory)

This is an integer value representing the minute of the time to be set.

4

seconds(Optional)

This is an integer value representing the seconds of the time to be set.

5

microseconds(Optional)

This is an integer value representing the microseconds of the time to be set.


Return Value

This function returns the DateTime object with modified (time) value. In the case of a failure, this function returns FALSE.

PHP Version

This function was first introduced as part of core PHP v 5.2.0 and, it works with all the later versions.

Example1

Following example illustrates the usage of the PHP date_time_set() function -

<?php
   //Creating a date
   $date = new DateTime();
   //Setting the date
   date_time_set($date, 7, 20, 35);   
   print("Date: ".date_format($date, "Y/m/d H:i:s"));
?>

Output

When the above code is executed, it will produce the following result -

Date: 2020/05/10 07:20:35

Example2

The following example creates a DateTime object and then modifies its value by using the date_time_set() function -

<?php
   //Date string
   $date_string = "25-09-1989 10:42:12";
   //Creating a DateTime object
   $date_time_Obj = date_create($date_string);
   print("Original Date: ".date_format($date_time_Obj, "Y/m/d H:i:s"));
   print("\n");
   //Setting the date
   $date = date_time_set($date_time_Obj, 6, 36, 3 );   
   print("Modified Date: ".date_format($date, "Y/m/d H:i:s"));
?>

Output

When the above code is executed, it will produce the following result -

Original Date: 1989/09/25 10:42:12
Modified Date: 1989/09/25 06:36:03

Example3

While invoking this built-in function if you pass the day and month values exceeding their range, then they will be added to their parent values -

<?php
   //Creating a date
   $date = new DateTime();
   //Setting the date
   date_time_set($date, 24, 15, 36);   
   print("Date: ".date_format($date, "Y/m/d H:i:s"));
?>

Output

Since we have set the month value as 15, three months are added to the appropriate time -

Date: 2020/05/11 00:15:36

Example4

Try the following example -

$dateSrc = '2007-04-19 12:50 GMT';
$dateTime = date_create( $dateSrc);;
$retval = date_time_set( $dateTime, 20, 40, 10);
   
echo 'DateTime::format(): '.$dateTime->format('Y:M:D:H:i:s');
echo "\n";

# Using second function.
$dateTime = new DateTime($dateSrc);
$retval = $dateTime->setTime(20, 56,6);
   
echo 'DateTime::format(): '.$dateTime->format('Y:M:D:H:i:s');

Output

When the above code is executed, it will produce the following result -

DateTime::format(): 2007:Apr:Thu:20:40:10
DateTime::format(): 2007:Apr:Thu:20:56:06


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the date_timezone_get() Function in PHP.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain