我想检查单个查询在特定时刻有多少用户在线。事情是我有时间保存为DATETIME(在在线字段中),我想将其与time()进行比较。下面是代码:
$online_margin = 10; //in minutes;
$online_margin = $online_margin*60;
$difference = time() - $online_margin;
$query = "SELECT id FROM users WHERE online > $difference";
我已经尝试使用convert()和cast但失败了,所以我很感激一些帮助为什么…
在与db字段比较之前,您需要将其转换为Y-m-d h:i:s
格式,
$online_margin = 10; //in minutes;
$online_margin = $online_margin*60;
$difference = time() - $online_margin;
$difference = date("Y-m-d h:i:s",$difference); //Convert seconds to Y-m-d h:i:s format
$query = "SELECT id FROM users WHERE online > $difference";
可选:您可以将列online
转换为unix_time进行比较,
$query = "SELECT id FROM users WHERE UNIX_TIMESTAMP(online) > $difference";
这比下面的答案效率低,但应该可以工作。
$query = "SELECT id FROM users WHERE UNIX_TIMESTAMP(online) > $difference"