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.

我試圖將我的MySQL數據庫中的datetime顯示為iso 8601格式的PHP字符串,但它出了問題。

17 Oct 2008 is coming out as: 1969-12-31T18:33:28-06:00 which is clearly not correct (the year should be 2008 not 1969)

2008年10月17日公布的日期為:1969-12- 31t18:33 - 33:28-06:00這顯然是不正確的(2008年應該是2008年而不是1969年)

This is the code I'm using:

這是我使用的代碼:

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

$post[3] is the datetime (CURRENT_TIMESTAMP) from my MySQL database.

$post[3]是我的MySQL數據庫中的datetime (CURRENT_TIMESTAMP)。

Any ideas what's going wrong?

有什么問題嗎?

5 个解决方案

#1

64

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

日期的第二個參數是UNIX時間戳,而不是數據庫時間戳字符串。

You need to convert your database timestamp with strtotime.

您需要使用strtotime轉換數據庫時間戳。

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

#2

26

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

使用PHP version 5.2中提供的DateTime類,可以這樣做:

$datetime = new DateTime('17 Oct 2008');

echo $datetime->format('c');

看到它在行動

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

對於PHP 5.4,您可以使用一行代碼:

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

#3

8

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.

1)你也可以用“Y-m-d\TH:i:sP”來代替“c”。

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 :

2)您的輸入的默認時區是服務器的時區。如果希望輸入的時區不同,則需要顯式地設置時區。然而,這也將影響您的輸出:

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 :

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

6

For pre PHP 5:

PHP 5之前:

function iso8601($time=false) {

if(!$time) $time=time();

return date("Y-m-d", $time) . 'T' . date("H:i:s", $time) .'+00:00';

}

#5

6

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

這是pre 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));

}

php iso8601 gmt,如何使用PHP以iso 8601格式顯示日期相关推荐

  1. python 时间戳转iso 8601_python - 如何解析ISO 8601格式的日期?

    python - 如何解析ISO 8601格式的日期? 这个问题在这里已有答案: 使用strftime将python datetime转换为epoch                          ...

  2. 如何获取具有日期,小时和分钟的ISO 8601格式的当前时刻?

    本文翻译自:How to get current moment in ISO 8601 format with date, hour, and minute? What is the most ele ...

  3. python中时间转换错误:时间戳转换|带有时区的转换 如何解析ISO 8601格式的日期? 2018-06-25T20:59:31.757+08:00

    有个JAVA接口 返回的一个时间为2018-06-25T20:59:31.757+08:00 查询了一下 他是一个ISO 8601格式 想要的结果是 2018-06-25 20:59:31 于是乎找了 ...

  4. ISO 8601 格式是干什么的?底层原理是什么?

    ISO 8601 是国际标准化组织(ISO)制定的一种表示日期和时间的格式标准.该标准的目的是提供一种标准的.易于理解和可互操作的日期和时间表示方法,以便于不同系统之间的数据交换和处理.ISO 860 ...

  5. python 日期解析_如何在Python中解析ISO 8601格式的日期?

    python 日期解析 Python provides a datetime standard library which introduces datetime.isoformat(). As pe ...

  6. python时间间隔标准化输出_利用Python将时间或时间间隔转为ISO 8601格式方法示例...

    前言 大家都知道,Python自带的datetime库提供了将datetime转为ISO 8610格式的函数,但是对于时间间隔(inteval)并没有提供转换的函数,下面我们动手写一个. 下面话不多说 ...

  7. 关于将ISO 8601格式的时间字符串转化为yyyy-MM-dd hh:mm:ss格式字符串用于前后台传输数据方法...

    给Date类型的对象绑定方法 Date.prototype.Format = function (fmt) { //author: meizzvar o = {"M+": this ...

  8. 将符合ISO 8601的字符串转换为java.util.Date

    我正在尝试将ISO 8601格式的String转换为java.util.Date . 如果与区域设置(比较示例)一起使用,我发现模式yyyy-MM-dd'T'HH:mm:ssZ符合ISO8601. 但 ...

  9. 了解一下ISO 8601是什么

    上周的组内分享,有朋友介绍一个工具包生成的日期是UTC,需要转成北京时,另外还带了Z,很是不解,组长介绍说这是ISO 8601的日期格式标准. 以前写过一些数据同步的服务,某些客户发送的数据就采用的U ...

最新文章

  1. 算法-----数组------合并两个有序数组
  2. heroku能用mysql吗_heroku连接到mysql数据库
  3. SQLyog连接虚拟机中mysql8.0详解,2003、1130、2058错误码解决
  4. 腾讯医疗AI实验室:3篇论文被国际顶尖会议收录
  5. C++全局变量和局部变量有什么区别
  6. 先来先服务算法代码_一致性哈希算法编写
  7. MySQL数据库概述
  8. 服务器ip算是虚拟资产吗,云服务器算资产吗
  9. python实现一个简单的图片浏览器
  10. DEV、SIT、UAT、PRD的意思
  11. 使用dd命令完整拷贝系统分区到另外一个硬盘上
  12. 【论文简述及翻译】RAFT: Recurrent All-Pairs Field Transforms for Optical Flow(ECCV 2020)
  13. 空间搜索(圆范围)中Geohash编码方案和格网编码方案对比探讨
  14. doctrine安装和配置
  15. 跟领导关系再好,也别做3件小事,嘴欠手贱,煮熟鸭子会飞
  16. BBC:关于睡眠你应该知道的十件事
  17. 马云除了发20亿玩红包,支付宝还干了一件轰动全球的大事儿!
  18. android jnl的mk文件,动态语言与静态语言
  19. open vas简介
  20. iar one or more breakpoints be set

热门文章

  1. EPOLL 事件之 EPOLLRDHUP
  2. 数字化转型是什么?核心又是什么呢?
  3. 解密迈向量产的百度Apollo自动驾驶技术与产品
  4. IBM收购Clearleap,提供云视频服务
  5. python学习笔记(五岁以下儿童)深深浅浅的副本复印件,文件和文件夹
  6. ueditor1.4.3配置过程(包含单独上传文件以及图片的使用)
  7. C# 枚举在项目中的应用总结
  8. Android判断Service是否运行
  9. IPSec逻辑体系架构
  10. [原创].怎样制作一个简单ip,以方便在Quartus II和Nios II中使用?