How to display a date as iso 8601 format with PHP

后端 未结 6 2091
遇见更好的自我
遇见更好的自我 2020-11-27 17:02

I\'m trying to display a datetime from my MySQL database as an iso 8601 formated string with PHP but it\'s coming out wrong.

17 Oct 2008 is coming out as: 1969-12-31

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 17:34

    Using the DateTime class available in PHP version 5.2 it would be done like this:

    $datetime = new DateTime('17 Oct 2008');
    echo $datetime->format('c');
    

    See it in action

    As of PHP 5.4 you can do this as a one-liner:

    echo (new DateTime('17 Oct 2008'))->format('c');
    

提交回复
热议问题