PHP Date Time Set Function
0 2889
To set the time in date you just have to use "date_time_set" function. This function is an inbuilt function. Let's see how this function works.
date_time_set function has five parameters. These are:-
date_time_set($object, $hour, $minute, $second, $microsecond)
- object: This parameter is mandatory and note that before using this object, date_create() function is used to set the DateTime Object.
- hour: This parameter is mandatory. It is used to set hour of time.
- minute: This parameter is mandatory. It is used to set minute of time.
- second: As per its name defines that it is used to set second of time. Not mandatory
- microsecond: As per, its name defines that it is used to set microsecond of time. Not mandatory
After using the date_time_set function a date_format function is used so that you could set the format in which you want to show your date and time.
Now let's see the examples:
<?php // First create DateTime object $create = date_create('2019-11-25'); // Now set a new DateTime date_time_set($create, 10, 30); echo date_format($create,"Y-m-d H:i:s"); ?>
Result:
Also, note that in date_time_set function seconds and microseconds parameters are set as zero by default.
In the below example I set seconds.
<?php $create = date_create('2019-11-25'); date_time_set($create, 10, 30, 45); echo date_format($create,"Y-m-d H:i:s"); ?>
Result:
Here I'm also going to set the fifth one parameter that is microsecond.
<?php $create = date_create('2019-11-25'); date_time_set($create, 10, 30, 45, 300); echo date_format($create,"Y-m-d H:i:s:u"); ?>
Result:
I hope this date time function blog helped you. If you have faced any issue, you can share your query with us and also if I have missed any important thing, you can comment below.
Share this blog with your friends and comment below how to do you like my blog.
Share:
Comments
Waiting for your comments