PHP Datetime 无法转换负 ISO8601 日期

标签 php datetime iso8601

用 DateTime 转换负日期让我出错

测试代码

var_dump(\DateTime::createFromFormat(\DateTime::ISO8601, '-0001-11-30T00:00:00+0100'));

结果

boolean false

预期结果

object(DateTime)[5]
  public 'date' => string '-0001-11-30 00:00:00' (length=20)
  public 'timezone_type' => int 1
  public 'timezone' => string '+01:00' (length=6)

(或类似的东西)

附言负日期字符串是用 $d->format(\DateTime::ISO8601);

创建的

PHP 版本 PHP 5.4.28

最佳答案

根据Format的手册

 public string DateTime::format ( string $format )

在哪里

$format

Format accepted by date().

如果你查看date()

$d->format(\DateTime::ISO8601);

应该是

$d->format("c")

因为“c”符合 date() 中的格式

ISO 8601 date (added in PHP 5)

在我的测试中 $d->format(\DateTime::ISO8601); 输出

2015-09-15T00:00:00+0200

同时

$d->format("c");

输出

2015-09-15T00:00:00+02:00

希望对你有帮助。

关于PHP Datetime 无法转换负 ISO8601 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31827328/

相关文章:

php - 如何使用 Ones 数据搜索两个 Mysql 表

PHP/MySQLi - 循环数组以构建动态 WHERE 子句

python - Pandas 按日期时间分组

java - 在 Time4j 中迭代给定范围的重复时间戳

c# 将日期时间对象转换为 iso 8601 字符串

javascript - 唯一识别传单标记

php - 将自定义 Docker 容器链接到 MySQL

python - 以天数表示的日期差异

c# - ISO 8601 值的解析日期 24 :00:00 fails

timezone - 日期是否在 ISO 8601 的目标时区?