Convert from MySQL datetime to another format with PHP

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

    To correctly format a DateTime object in PHP for storing in MySQL use the standardised format that MySQL uses, which is ISO 8601.

    PHP has had this format stored as a constant since version 5.1.1, and I highly recommend using it rather than manually typing the string each time.

    $dtNow = new DateTime();
    $mysqlDateTime = $dtNow->format(DateTime::ISO8601);
    

    This, and a list of other PHP DateTime constants are available at http://php.net/manual/en/class.datetime.php#datetime.constants.types

提交回复
热议问题