假设我21:07:35现在有这个时间,这次21:02:37是这样的变量
<?php
$current_time = "21:07:35";
$passed_time = "21:02:37";
?>
Run Code Online (Sandbox Code Playgroud)
现在我想检查是否$current_time小于5 minutes那么回声You are online
所以我怎么能用PHP做到这一点?
谢谢
:)
小智 5
要将给定时间与当前时间进行比较:
if (strtotime($given_time) >= time()+300) echo "You are online";
Run Code Online (Sandbox Code Playgroud)
300是您要检查的秒数差异.在这种情况下,5分60秒.
如果要比较两个任意时间,请使用:
if (strtotime($timeA) >= strtotime($timeB)+300) echo "You are online";
Run Code Online (Sandbox Code Playgroud)
请注意:如果时间在不同的日期,例如星期五23:58和星期六00:03,这将失败,因为您只是将时间作为变量传递.你最好先存储和比较Unix时间戳.