0

I'm trying to parse following string:

 Thu Oct 03 2013 07:03:41 GMT+0200 (Central Europe Standard Time)

But I'm struggling to find the corresponding format, I tried:

  $date= DateTime::createFromFormat('D M d Y H:i:s eO (*)','Thu Oct 03 2013 07:03:41 GMT+0200 (Central Europe Standard Time)');
  echo $date->format('Y-m-d');

Which results in error. Problem is, that there is no space between GMT+0200 and the brackets. Following works just fine

$date= DateTime::createFromFormat('D M d Y H:i:s e O','Thu Oct 03 2013 07:03:41 GMT +0200');
echo $date->format('Y-m-d');

But (obviously) I should be able to parse also the first example. So do you have any suggestion how the correct format should look like?

the error I get:

Fatal error: Call to a member function format() on a non-object in C:\....

var_dump of $date before calling $date->format:

 boolean false
4

2 回答 2

1

您可以拆分传入的字符串。这是给这只特殊猫剥皮的一种方法:-

$dateString = 'Thu Oct 03 2013 07:03:41 GMT+0200(Central Europe Standard Time)';
\DateTime::createFromFormat('D M d Y H:i:s O', explode('(', $dateString)[0]);

有关更多帮助,请参见http://php.net/date

看到它工作

于 2013-10-04T15:25:41.417 回答
1

问题是格式字符串,应该是

D M d Y H:i:s e+

我替换eO为只是e因为输入包含GMT+0200,它在“GMT”和偏移量之间没有分隔符。我还用 替换了该(*)部分+,这是唯一可以消耗可变数量输入的说明符(*匹配一个标记,即一个单词——如果之后有更多输入,则解析失败)。

+注意,由于使用(use to see it),仍然会有警告DateTime::getLastErrors,但转换会正常工作。

于 2013-10-04T15:31:57.510 回答