gpt4 book ai didi

PHP:strtotime 返回类型为 "nothing"的 "boolean"

转载 作者:可可西里 更新时间:2023-10-31 22:44:04 24 4
gpt4 key购买 nike

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

谁能解释一下这个以及如何避免 bool 结果?

最佳答案

来自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!

关于PHP:strtotime 返回类型为 "nothing"的 "boolean",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19164923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com