简体   繁体   English

strtotime返回假

[英]strtotime returning false

I have datestrings that looks like this: 我有看起来像这样的日期字符串:

"2012 28 Nov 21:00 CET"

strtotime("2012 28 Nov 21:00 CET") return false. strtotime("2012 28 Nov 21:00 CET")返回false。

Is there any way to convert this string in to a date object, or do I need to format it differently? 有什么办法可以将该字符串转换为日期对象,还是需要以其他方式设置其格式?

2012 28 Nov 21:00 CET is weird date/time format. 2012 28 Nov 21:00 CET是奇怪的日期/时间格式。 YDM? YDM? Where are you getting that? 你从哪儿得到的?

At any rate, the DateTime object has a method createFromFormat that will do a better job parsing that: 无论如何,DateTime对象都有一个createFromFormat方法,它将更好地解析以下内容:

$dt = DateTime::createFromFormat("Y d M H:i T", '2012 28 Nov 21:00 CET');
$ts = $dt->getTimestamp();
echo $ts; // 1354132800 

Try it here: http://codepad.viper-7.com/NfAmcw 在这里尝试: http : //codepad.viper-7.com/NfAmcw

strtotime expects a "English textual datetime" (according to the manual ), which YDM is not. strtotime期望使用“英文文本日期时间” (根据手册 ),而YDM则不是。 Any time strtotime returns false, it simply doesn't understand your string, which in this application is expected. 任何时候strtotime返回false,它根本就无法理解您的字符串,这在此应用程序中是预期的。 A note on the manual page deals with this issue: 手册页上的注释说明了此问题:

Note: 注意:

Dates in the m/d/y or dmy formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; 通过查看各个组成部分之间的分隔符,可以消除m / d / y或dmy格式的日期的歧义:如果分隔符为斜杠(/),则假定为美国m / d / y; whereas if the separator is a dash (-) or a dot (.), then the European dmy format is assumed. 相反,如果分隔符是破折号(-)或点(。),则采用欧洲dmy格式。

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible. 为避免潜在的歧义,最好尽可能使用ISO 8601(YYYY-MM-DD)日期或DateTime :: createFromFormat()。

That's just as well, DateTime is a superior tool for any interaction with dates or times. 同样, DateTime是与日期或时间进行任何交互的出色工具。

Documentation 文献资料

strtotime doesn't create a date object, it simply tries to parse your input and returns a unix timestamp. strtotime不会创建日期对象,它只是尝试解析您的输入并返回unix时间戳。

You can find valid formats here: http://www.php.net/manual/en/datetime.formats.php 您可以在此处找到有效的格式: http//www.php.net/manual/en/datetime.formats.php

echo strtotime("2012-11-28T21:00:00+01:00");

would output 1354132800 将输出1354132800

to create a php DateTime object you could do this: 创建一个PHP DateTime对象,您可以这样做:

$date = new DateTime('2012-11-28T21:00:00+01:00');
echo $date->format('Y-m-d H:i:s');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM