[英]How to check if it's more that 2 days in the past not the future?
I want to check if the date is more that or equals 2 days in the past only , But it also works if it's in the future. 我只想检查日期是否大于或等于过去2天,但是如果将来也可以使用。
Here is the code: 这是代码:
$d = new DateTime('04-06-2018');
$todayDate = new DateTime();
$interval = $d->diff($todayDate);
$days = $interval->format('%a');
if($days >= 2)
{echo 'true'; }
else
{echo 'false';}
It returns true whenever the difference between the two dates is more that or equals 2 , How to solve that? 每当两个日期之间的差大于或等于2时,它将返回true,如何解决呢?
You can do this by checking invert
property in your DateInterval
. 您可以通过检查DateInterval
invert
属性来执行此操作。
$d = new DateTime('3-04-2018');
$todayDate = new DateTime();
$interval = $d->diff($todayDate);
if($interval->format('%a') >= 2
&& 0 === $interval->invert
) {
echo 'true';
} else {
echo 'false';
}
扩展是否检查日期是否为过去:
if($days >= 2 && strtotime($d) < time())
There is one more way to acheive this 还有另一种方法可以实现这一目标
return $interval->format("%r%a"); 返回$ interval-> format(“%r%a”);
Cast to int if needed: 如果需要,则转换为int:
return (int)$interval->format("%r%a"); return(int)$ interval-> format(“%r%a”);
Here you will get positive and negative number to check future and past date. 在这里,您将获得正数和负数以检查将来和过去的日期。
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.