将两个时间值相加并与其他时间进行比较


add two time values and comparing with other time

我有两个时间值,说开始时间= 23:00:00和估计时间= 00:

45:00 现在我想添加开始时间和估计时间并将其存储在我喜欢的变量中

$add = abs(strtotime($starttime) + strtotime($estimationtime));

现在我必须比较一个新的时间,比如当前时间或任何其他时间间隔 (23:50:00)

if($time >=$starttime && $time <=$add)
{
$message=array("message"=>"there is an appointment");
}

如果我回显$add我没有得到正确的格式,它会显示2675756700请帮助我解决这个问题

如果你想以可读的形式回显你的$add变量,你必须先格式化它。

echo(date("H:i:s", $add));

strtotime() 返回一个时间戳,这是自 1970 年 1 月 1 日以来的秒数。您无法将其与格式化的时间值进行比较 - 它只是一个整数。

所以基本上,你正在做等效的事情:

$add = 1 + 2; // 3
if (3 >= '23:50:00') ...

如果要将时间值作为时间戳进行比较,则必须将所有内容转换为时间戳。或者,您可以使用 DateTime 对象,它会自动为您处理很多此类事情。