PHP Datetime无法转换否定的ISO8601日期 [英] PHP Datetime fail to convert negative ISO8601 date

查看:409
本文介绍了PHP Datetime无法转换否定的ISO8601日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



测试代码

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

结果

  boolean false 

预期结果

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

(或类似的东西)



PS使用$ d->格式(\DateTime :: ISO8601)创建负日期字符串;



PHP版本PHP 5.4.28

解决方案

根据的手册格式

  public string DateTime :: format(string $ format)

其中

  $ format 

date()接受的格式。

如果您查看 date()

  $ d->格式(\DateTime :: ISO8601); 

应该是

 code> $ d->格式(c)

因为c根据 date()的格式


ISO 8601日期(以PHP 5添加)


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


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


  $ d-> format(c); 

输出


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


我希望它有帮助。


Converting a negative date with DateTime get me false

Test code

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

Result

boolean false

Expected result

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)

(or something similar)

P.S. The negative date string was created with $d->format(\DateTime::ISO8601);

PHP version PHP 5.4.28

解决方案

According to manual of Format

 public string DateTime::format ( string $format )

where

$format

Format accepted by date().

and if you look in date()

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

should be

$d->format("c")

because "c" is according to format in date()

ISO 8601 date (added in PHP 5)

In my test $d->format(\DateTime::ISO8601); outputs

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

while

$d->format("c");

outputs

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

I hope it helps.

这篇关于PHP Datetime无法转换否定的ISO8601日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆