Convert from MySQL datetime to another format with PHP

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

    Depending on your MySQL datetime configuration. Typically: 2011-12-31 07:55:13 format. This very simple function should do the magic:

    function datetime()
    {
        return date( 'Y-m-d H:i:s', time());
    }
    
    echo datetime(); // display example: 2011-12-31 07:55:13
    

    Or a bit more advance to match the question.

    function datetime($date_string = false)
    {
        if (!$date_string)
        {
            $date_string = time();
        }
        return date("Y-m-d H:i:s", strtotime($date_string));
    }
    

提交回复
热议问题