How to change date time format in php?
I know this is old, but, in running into a vendor that inconsistently uses 5 different date formats in their APIs (and test servers with a variety of PHP versions from the 5's through the latest 7's), I decided to write a universal converter that works with a myriad of PHP versions. Show This converter will take virtually any input, including any standard datetime format (including with or without milliseconds) and any Epoch Time representation (including with or without milliseconds) and convert it to virtually any other format. To call it:
Sending null for the format will make the function return the datetime in Epoch/Unix Time. Otherwise, send any format string that date() supports, as well as with ".u" for milliseconds (I handle milliseconds as well, even though date() returns zeros). Here's the code:
Here's some sample calls - you will note it also handles any time zone data (though as noted above, any non GMT time is returned in your time zone).
Here's the output:
The only thing not in this version is the ability to select the time zone you want the returned datetime to be in. Originally, I wrote this to change any datetime to Epoch Time, so, I didn't need time zone support. It's trivial to add though. How do I change from 24 hour format to 12 hour format in PHP?Similarly, H will give you the hour in 24-hour format with leading zeros, but h will give you the hour in 12-hour format with leading zeros.
How can change time in 24 hour format in PHP?$time = '23:45'; echo date('g:i a', strtotime($time));
How can I get current date in YYYY$date = date("yyyy-mm-dd", strtotime(now));
How can I change the date format of a datetime?Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.
|