我有变量$EDate
,我使用 strtotime 函数将此变量具有不同的值,结果如下:
$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
有人能解释一下这一点以及如何避免布尔结果吗?
来自文档 http://php.net/strtotime:
m/d/y 或 d-m-y 格式的日期可通过查看各个部分之间的分隔符来消除歧义:如果分隔符是斜杠 (/),则假定为美式 m/d/y;然而如果分隔符是破折号 (-) 或点 (.),则采用欧洲 d-m-y 格式。
您的代码假设日期位于d-m-y
格式,并将返回FALSE
由于月份值不正确:
var_dump(strtotime('10-21-2013')); // no month 21
var_dump(strtotime('09-30-2013'));
var_dump(strtotime('09-30-2013'));
如果您希望能够使用自定义格式,请使用DateTime::createFromFormat() http://php.net/manual/en/datetime.createfromformat.php反而:
$date = DateTime::createFromFormat('m-d-Y', '10-21-2013');
echo $date->format('U');
Demo! https://eval.in/52378
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)