【问题标题】:How to display a date as iso 8601 format with PHP如何使用 PHP 将日期显示为 iso 8601 格式
【发布时间】:2010-10-28 12:59:03
【问题描述】:

我正在尝试使用 PHP 将我的 MySQL 数据库中的日期时间显示为 iso 8601 格式的字符串,但它出现了错误。

2008 年 10 月 17 日出现为:1969-12-31T18:33:28-06:00,这显然不正确(年份应该是 2008 年而不是 1969 年)

这是我正在使用的代码:

<?= date("c", $post[3]) ?>

$post[3] is the datetime (CURRENT_TIMESTAMP) 来自我的 MySQL 数据库。

有什么想法吗?

【问题讨论】:

    标签: php mysql date date-format time-format


    【解决方案1】:

    date 的第二个参数是 UNIX 时间戳,而不是数据库时间戳字符串。

    您需要将数据库时间戳转换为strtotime

    <?= date("c", strtotime($post[3])) ?>
    

    【讨论】:

    • 请使用类!看看@John Conde 解决方案,;)
    • date() 函数会降低准确性,请改用 John 的解决方案!
    • @Christian 什么是“日期()函数降低准确性”?
    • 它不能正确处理毫秒(和更小的单位)。
    • @Christian 'c' 格式不包括秒的小数部分,John 的答案目前仅使用 'c' 格式,因此 John 的答案不是改进。没错, DateTime() 允许更高的分辨率,但除非您使用具有更高分辨率的自定义 8601 格式,否则它对您没有帮助。所以,如果你只使用'c'格式并且你喜欢函数式编程,那么我认为这个解决方案很好。对于那些想要更高分辨率的人,Clary 的回答提供了一些示例。
    【解决方案2】:

    使用 PHP 5.2 版中的DateTime class 可以这样完成:

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

    从 PHP 5.4 开始,您可以将其作为单行来执行:

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

    【讨论】:

      【解决方案3】:

      程序风格:

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

      面向对象的风格:

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

      混合 1:

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

      混合 2:

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

      注意事项:

      1) 您也可以使用'Y-m-d\TH:i:sP' 作为'c' 的替代格式。

      2) 您输入的默认时区是您的服务器的时区。如果您希望输入用于不同的时区,则需要明确设置您的时区。但是,这也会影响您的输出:

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

      3) 如果您希望输出的时区与输入的时区不同,您可以明确设置您的时区:

      echo date_format(date_create('17 Oct 2008')->setTimezone(new DateTimeZone('America/New_York')), 'c');
      // Output : 2008-10-16T18:00:00-04:00
      

      【讨论】:

        【解决方案4】:

        对于 PHP 5 之前的版本:

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

        【讨论】:

        • 为什么不转义 T 符号...并且 !$time 条件不是必需的,因为当没有给出时间参数或 null 时使用当前时间: date('Ymd\TH:i:s \Z', $time);//Z代表当前时区
        【解决方案5】:

        这是 PHP 5 之前的好功能: 我在最后添加了 GMT 差异,它不是硬编码的。

        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));
        }
        

        【讨论】:

          【解决方案6】:

          问题多次出现在 4 或 8 决赛中的毫秒和最后微秒。要将 DATE 转换为 ISO 8601 "date(DATE_ISO8601)",这些是适合我的解决方案之一:

          // 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
          

          【讨论】:

          • 对于任何来这里寻找 Swift ISO8601DateFormatter 和您的 PHP 服务器日期格式之间的解决方案的人 - 上面的工作是 $dt-&gt;format('Y-m-d\TH:i:s.').substr($dt-&gt;format('u'),0,3).'Z'; 谢谢你!!!
          猜你喜欢
          • 1970-01-01
          • 2017-06-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多