PHP:strtotime 返回“无";“布尔"类型的 [英] PHP: strtotime returns "nothing" of type "boolean"
问题描述
我有变量 $EDate
,我使用 strtotime 函数和这个变量不同的值,结果如下:
I have the variable $EDate
, I used strtotime function with this variable with different values and the result was as following:
$EDate = 10-21-2013; echo "strtotime($EDate)"; the result = nothing and the type is boolean
$EDate = 09-02-2013; echo "strtotime($EDate)"; the result = 1360386000 and the type is integer
$EDate = 09-30-2013; echo "strtotime($EDate)"; the result = nothing and the type is boolean
$EDate = 09-30-2013; echo "strtotime($EDate)"; the result = nothing and the type is boolean
$EDate = 07-02-2014; echo "strtotime($EDate)"; the result = 1391749200 and the type is integer
$EDate = 10-12-2014; echo "strtotime($EDate)"; the result = 1418187600 and the type is integer
谁能解释一下这个以及如何避免布尔结果?
Can anybody explain this and how to avoid the boolean result?
推荐答案
来自文档:
m/d/y 或 d-m-y 格式的日期通过查看各个组件之间的分隔符来消除歧义:如果分隔符是斜杠 (/),则假定为美国 m/d/y;而如果分隔符是破折号 (-) 或点 (.),则假定为欧洲 d-m-y 格式.
Dates in the m/d/y or d-m-y 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; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
您的代码假定日期为 d-m-y
格式,并且会返回 FALSE
,因为月份值不正确:
Your code assumes that the date is in d-m-y
format, and will return FALSE
since the month values are incorrect:
var_dump(strtotime('10-21-2013')); // no month 21
var_dump(strtotime('09-30-2013'));
var_dump(strtotime('09-30-2013'));
如果您希望能够使用自定义格式,请使用 DateTime::createFromFormat()
代替:
If you want to be able to use custom formats, use DateTime::createFromFormat()
instead:
$date = DateTime::createFromFormat('m-d-Y', '10-21-2013');
echo $date->format('U');
这篇关于PHP:strtotime 返回“无";“布尔"类型的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!