php中返回空值的strotime
数据集值返回为空,日志文件上没有错误php中返回空值的strotime,php,datetime,strtotime,Php,Datetime,Strtotime,数据集值返回为空,日志文件上没有错误 $edate = trim($_POST['txtedate']); //user inputs date 12-01-2021 $int_effective_date = new DateTime(strtotime($edate)); echo "edate:- ".$edate."<br />"; echo "strtotime_edate:- ".strtotim
$edate = trim($_POST['txtedate']); //user inputs date 12-01-2021
$int_effective_date = new DateTime(strtotime($edate));
echo "edate:- ".$edate."<br />";
echo "strtotime_edate:- ".strtotime($edate)."<br />";
echo "dateset:- ".strtotime($int_effective_date->format('Y/m/d'));
$edate=trim($_POST['txtedate'])//用户输入日期:2021年1月12日
$int_effective_date=新日期时间(strotime($edate));
回声“edate:-”$edate.“
”;
回声“strotime_edate:-”.strotime($edate)。“
”;
echo“dateset:-”.strotime($int\u effective\u date->format('Y/m/d'));
结果:
edate:-2021-01-12
标准时间:-1610389800
日期集:-
解释一下@iainn:我不能100%确定为什么要在
DateTime
对象和对strotime
的函数调用之间来回更改
但是,我可以解释您的代码最可能出现的问题
strottime
首先,让我们澄清一下12-01-2021
的格式是(d-m-Y
)?希望是这样,在这种情况下,PHPsstrotime
函数能够正确理解它并生成一个Unix时间戳(即自1970年初以来经过的秒数)
DateTime
然后将该时间戳传递给DateTime
,但忽略了通知DateTime
它是什么类型的时间戳
$int_effective_date = new DateTime(strtotime($edate));
// Is the same as...
$int_effective_date = new DateTime(1610409600);
但是,DateTime
不会将您的时间戳视为不正确
,并尝试对其进行处理
HisYmd
HisY
Time => 16:10
Year => 9600
9600-02-05 16:10:40
strotime
调用
echo "dateset:- ".strtotime($int_effective_date->format('Y/m/d'));
// Is the same as...
echo "dateset:- ".strtotime("9600/02/05");
现在,strotime
将始终返回一些内容。这意味着第一个问题是您使用的是echo
,它不会输出(bool)false
尝试:
您可能会问,为什么在@El_Vanja的链接代码示例中不会发生这种情况
回答
我相信,答案是您的PHP版本不是最新的,超过32位
日期范围的任何内容都将从strotime
返回(bool)false
为了解决这个特定的问题,我建议您更新PHP版本(如果您还没有移动到64位,则更新操作系统!)
然而,除此之外,我强烈建议您坚持使用DateTime
对象/类。如果没有其他东西的话,它可以让你远离这些讨厌的虫子
供参考:
echo strtotime( (new DateTime("@1610409600"))->format("Y-m-d") ); // Output: 1610409600
echo strtotime( (new DateTime("2021-01-12"))->format("Y-m-d") ); // Output: 1610409600
您正在尝试将用户输入转换为时间戳,将日期时间对象转换为字符串,将时间戳转换为时间戳-为什么?具体地说,问题是您不能将unix时间戳传递给DateTime构造函数,但是如果您删掉许多中间步骤,所有这些都会更加清楚。@El_Vanja是的,为什么日期集的日期是Thu,2月3日6000 23:00 GMT?@iainn在他的评论中已经回答了这个问题,而不是2012年1月12日-
DateTime
没有将时间戳作为构造函数参数处理。@Steven Hm,是的,我完全忘了测试eol
版本。我一直忘了现在仍然使用古老的PHP。看起来像是5.2.0-5.2.5复制了这个。
echo "dateset:- ".strtotime($int_effective_date->format('Y/m/d'));
// Is the same as...
echo "dateset:- ".strtotime("9600/02/05");
var_dump(strtotime("9600/02/05"));
echo strtotime( (new DateTime("@1610409600"))->format("Y-m-d") ); // Output: 1610409600
echo strtotime( (new DateTime("2021-01-12"))->format("Y-m-d") ); // Output: 1610409600