Convert from MySQL datetime to another format with PHP

后端 未结 18 1196
醉话见心
醉话见心 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:28

    You can also have your query return the time as a Unix timestamp. That would get rid of the need to call strtotime() and make things a bit less intensive on the PHP side...

    select  UNIX_TIMESTAMP(timsstamp) as unixtime from the_table where id = 1234;
    

    Then in PHP just use the date() function to format it whichever way you'd like.

    unixtime);
    ?>
    

    or

    unixtime);
    ?>
    

    I like this approach as opposed to using MySQL's DATE_FORMAT function, because it allows you to reuse the same query to grab the data and allows you to alter the formatting in PHP.

    It's annoying to have two different queries just to change the way the date looks in the UI.

提交回复
热议问题