微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP:strtotime返回“boolean”类型的“nothing”

我有变量$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

任何人都可以解释这个以及如何避免布尔结果吗?

解决方法:

documentation

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:

var_dump(strtotime('10-21-2013')); // no month 21
var_dump(strtotime('09-30-2013'));
var_dump(strtotime('09-30-2013'));

如果您希望能够使用自定义格式,请使用DateTime::createFromFormat()

$date = DateTime::createFromFormat('m-d-Y', '10-21-2013');
echo $date->format('U');

Demo!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐