Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from MySQL datetime to another format with PHP

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?

like image 257
Tim Boland Avatar asked Sep 25 '08 23:09

Tim Boland


People also ask

How to change date format in PHP from MySQL?

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');

How convert date from yyyy mm dd to dd-mm-yyyy format in PHP?

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.

How can get date in dd-mm-yyyy format in MySQL?

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.

Which PHP function do you use to format date information?

The date_format() function returns a date formatted according to the specified format.


1 Answers

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)

like image 155
kta Avatar answered Oct 05 '22 18:10

kta