How to display a date as iso 8601 format with PHP

How to display a date as iso 8601 format with PHP

Asked on December 28, 2018 in PHP.
Add Comment


  • 3 Answer(s)

    UNIX timestamp is the second argument of date,

    We have to convert the database timestamp to strtotime() function.

    <?= date("c", strtotime($post[3])) ?>
    
    
    Answered on December 28, 2018.
    Add Comment

    The  DateTime class  can be used for display a date as iso 8601 format with PHP. It is available in the  PHP version 5.2 and later.

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

    If we have PHP 5.4 , achieve this simple one liner.

    echo (new DateTime('17 Oct 2008'))->format('c');
    
    Answered on December 28, 2018.
    Add Comment

    Here is the easy and better function for pre PHP 5.

    function iso8601($time=false) {
        if(!$time) $time=time();
        return date("Y-m-d", $time) . 'T' . date("H:i:s", $time) .'+00:00';
    }
    
    Answered on December 28, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.