ftvilla.blogg.se

Php date format options
Php date format options












php date format options

Note that if you want to input into mysql, the time format needs to be: format("Y-m-d H:i:s.u") $local = $now->setTimeZone(new DateTimeZone('Australia/Canberra')) Įcho $local->format("m-d-Y H:i:s.u"). The setTimeZone() method can be used to accomplish this requirement.Īs an example: $now = DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', '')) However, this should be done as a separate step after the initialisation ( not using the third parameter of createFromFormat()) because of the reasons discussed above. If you need to display the time for a particular time zone then you need to set it accordingly.

php date format options

This means that the DateTime object is implicitly initialised to UTC, which is fine for server internal tasks that just want to track elapsed time. However, the technique described here is initialising the DateTime object using microtime() which returns the number of seconds elapsed since the Unix Epoch ( 00:00:00 GMT). Normally the createFromFormat() method will use the local time zone if one is not specified. $now = DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', '')) Ī note on time zones in response to DaVe. 4 Answers Sorted by: 215 You don't need to turn the string into a timestamp in order to create the DateTime object (in fact, its constructor doesn't even allow you to do this, as you can tell). Too bad it doesn't feel quite as elegant any more. Thanks goes to giggsey for pointing out a flaw in my original answer, adding number_format() to the line should fix the case of the exact second.

#Php date format options manual#

This produces the following output: 04-13-2015 05:56:22.082300įrom the PHP manual page for date formats: $now = DateTime::createFromFormat('U.u', microtime(true)) You can readily do this this with the input format U.u.














Php date format options