I have a datetime
column in MySQL.
How can I convert it to the display as mm/dd/yy H:M (AM/PM) using PHP?
You can do that on the PHP side or on the MySQL side. On the MySQL side you could use DATE_FORMAT : SELECT DATE_FORMAT(NOW(), '%d-%m-%Y');
Answer: Use the strtotime() Function You can first use the PHP strtotime() function to convert any textual datetime into Unix timestamp, then simply use the PHP date() function to convert this timestamp into desired date format. The following example will convert a date from yyyy-mm-dd format to dd-mm-yyyy.
The MySQL DATE_FORMAT() function formats a date value with a given specified format. You may also use MySQL DATE_FORMAT() on datetime values and use some of the formats specified for the TIME_FORMAT() function to format the time value as well. Let us take a look at the syntax of DATE_FORMAT() and some examples.
The date_format() function returns a date formatted according to the specified format.
If you're looking for a way to normalize a date into MySQL format, use the following
$phpdate = strtotime( $mysqldate ); $mysqldate = date( 'Y-m-d H:i:s', $phpdate );
The line $phpdate = strtotime( $mysqldate )
accepts a string and performs a series of heuristics to turn that string into a unix timestamp.
The line $mysqldate = date( 'Y-m-d H:i:s', $phpdate )
uses that timestamp and PHP's date
function to turn that timestamp back into MySQL's standard date format.
(Editor Note: This answer is here because of an original question with confusing wording, and the general Google usefulness this answer provided even if it didnt' directly answer the question that now exists)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With