我正在处理电子邮件链接,一段时间后就会过期X分钟,其中X表示一些随机日期时间。所以我的动机是在一段时间后使链接过期,无论我在其中设置 date_time$过期日期.
所以我自己创建了虚拟代码只是为了确保我的代码是否有效。
$currentDateTime = new \DateTime();
$currentDateTime-> setTimezone(new \DateTimeZone('Asia/kolkata'));
$now = $currentDateTime-> format(' h:iA j-M-Y ');
$expire_date = "02:59PM 26-Mar-2019";
if($now > $expire_date)
{
echo " link is expired";
}
else{
echo " link still alive ";
}
我想我在上面的代码中遗漏了一些东西,不知何故,上面的代码不起作用,如果有人指出正确的方向或一些更好的实现,那就太好了。
您将时间作为字符串进行比较。这不起作用,因为您的第一个格式化字符串有一个前导空格。
相反,尝试删除空格,或者更好地将时间作为 DateTime 对象进行比较:
$timezone = new \DateTimeZone('Asia/kolkata');
// Create the current DateTime object
$currentDateTime = new \DateTime();
$currentDateTime-> setTimezone($timezone);
// Create the given DateTime object
$expire_date = "02:59PM 26-Mar-2019";
$expireDateTime = \DateTime::createFromFormat($expire_date, 'h:iA j-M-Y');
// Compare the objects
if($currentDateTime > $expireDateTime)
{
echo " link is expired";
}
else{
echo " link still alive ";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)