How to display a date as iso 8601 format with PHP

后端 未结 6 2078
遇见更好的自我
遇见更好的自我 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:16

    The second argument of date is a UNIX timestamp, not a database timestamp string.

    You need to convert your database timestamp with strtotime.

    <?= date("c", strtotime($post[3])) ?>
    
    0 讨论(0)
  • 2020-11-27 17:24

    Here is the good function for pre PHP 5: I added GMT difference at the end, it's not hardcoded.

    function iso8601($time=false) {
        if ($time === false) $time = time();
        $date = date('Y-m-d\TH:i:sO', $time);
        return (substr($date, 0, strlen($date)-2).':'.substr($date, -2));
    }
    
    0 讨论(0)
  • 2020-11-27 17:30

    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';
    }
    
    0 讨论(0)
  • 2020-11-27 17:31

    Procedural style :

    echo date_format(date_create('17 Oct 2008'), 'c');
    // Output : 2008-10-17T00:00:00+02:00
    

    Object oriented style :

    $formatteddate = new DateTime('17 Oct 2008');
    echo $datetime->format('c');
    // Output : 2008-10-17T00:00:00+02:00
    

    Hybrid 1 :

    echo date_format(new DateTime('17 Oct 2008'), 'c');
    // Output : 2008-10-17T00:00:00+02:00
    

    Hybrid 2 :

    echo date_create('17 Oct 2008')->format('c');
    // Output : 2008-10-17T00:00:00+02:00
    

    Notes :

    1) You could also use 'Y-m-d\TH:i:sP' as an alternative to 'c' for your format.

    2) The default time zone of your input is the time zone of your server. If you want the input to be for a different time zone, you need to set your time zone explicitly. This will also impact your output, however :

    echo date_format(date_create('17 Oct 2008 +0800'), 'c');
    // Output : 2008-10-17T00:00:00+08:00
    

    3) If you want the output to be for a time zone different from that of your input, you can set your time zone explicitly :

    echo date_format(date_create('17 Oct 2008')->setTimezone(new DateTimeZone('America/New_York')), 'c');
    // Output : 2008-10-16T18:00:00-04:00
    
    0 讨论(0)
  • 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');
    
    0 讨论(0)
  • 2020-11-27 17:37

    The problem many times occurs with the milliseconds and final microseconds that many times are in 4 or 8 finals. To convert the DATE to ISO 8601 "date(DATE_ISO8601)" these are one of the solutions that works for me:

    // In this form it leaves the date as it is without taking the current date as a reference
    $dt = new DateTime();
    echo $dt->format('Y-m-d\TH:i:s.').substr($dt->format('u'),0,3).'Z';
    // return-> 2020-05-14T13:35:55.191Z
    
    // In this form it takes the reference of the current date
    echo date('Y-m-d\TH:i:s'.substr((string)microtime(), 1, 4).'\Z');
    return-> 2020-05-14T13:35:55.191Z
    
    // Various examples:
    $date_in = '2020-05-25 22:12 03.056';
    $dt = new DateTime($date_in);
    echo $dt->format('Y-m-d\TH:i:s.').substr($dt->format('u'),0,3).'Z';
    // return-> 2020-05-25T22:12:03.056Z
    
    //In this form it takes the reference of the current date
    echo date('Y-m-d\TH:i:s'.substr((string)microtime(), 1, 4).'\Z',strtotime($date_in));
    // return-> 2020-05-25T14:22:05.188Z
    
    0 讨论(0)
提交回复
热议问题