Convert from MySQL datetime to another format with PHP

后端 未结 18 1233
醉话见心
醉话见心 2020-11-22 01:08

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?

18条回答
  •  星月不相逢
    2020-11-22 01:22

    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)

提交回复
热议问题