繁体   English   中英

php比较一小时内的时间戳+/-

[英]php comparing timestamps within an hour +/-

我写了一个有条件的文章,比较两个时间戳,如果控制变量和被比较变量之间的时间差在1个小时以内,则需要执行它。

现在看起来像这样,显然仅小于和等于:

if( ($departure - $item['time']) <= 3600) ): 

<=更改为<=>起作用吗?

<=>运算符将提供组合比较,因为它将:

Return 0 if values on either side are equal
Return 1 if value on the left is greater
Return -1 if the value on the right is greater

您要做的是比较$departure - $item['time']绝对值 $departure - $item['time']

if(abs($departure - $item['time']) <= 3600)): 

不,当一侧较小/相等/较大时, <=>运算符仅返回-1/0/1,并且仅在PHP7中有效。

实际上,在您的情况下,似乎$departure - $item['time']可能会变成负值(如果$item['time']大于$departure ),那么在以下情况下,如果if语句的计算结果为true,例如, $item['time']是将来$departure起两个,三个或更多个小时。

这样的东西行不行?

$diff = $departure - $item['time'];
if ($diff <= 3600 && $diff >= -3600)
{
    // do what you have to do
}

&& (AND)是逻辑运算符,这意味着两个较小的语句都必须求值为true,以便完整的语句为true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM