dongmei3498 2012-10-04 18:55
浏览 13
已采纳

PHP MySQL如果最新的时间戳比今天大两周

I'm trying to query a table where attendance records are kept per user and group. If the lastest timestamp per user and group is two weeks older than today's date, then an image will show. Here is the code I have at this point but I don't think I'm understanding the time formats or something.

$tsquery = mysql_query("SELECT ts FROM `attendance` WHERE contact_id='$contactid' AND             group_id='$groupid' AND `ts` + INTERVAL 7 DAY < NOW() ORDER BY ts LIMIT 1 ");
$ts=mysql_fetch_array($tsquery);

if(isset($ts)){
echo "<img src='images/care.png' title='Care Group' style='border: none; background-color:     #fff;'>";
} else {
} 

I'm getting false results on some people. If someone didn't attend an event 2 weeks ago, but did attend an event yesterday, it's returning true rather than finding that they were in attendance within the last two weeks. Is there a better way of doing this that would return more accurate results?

  • 写回答

1条回答 默认 最新

  • douwei3172 2012-10-04 18:58
    关注

    You need to turn your logic around a bit. Checking for the presence of rows matching ts + INTERVAL 7 DAY < NOW() means "did they attend any events more than 7 days ago"? Checking for rows matching ts + INTERVAL 7 DAY >= NOW() means "did they attend any events in the past week?" If there are no rows then they haven't done anything in a week.

    (Also, just a nitpick, but you have "two weeks" in prose and "7 days" in code. Two weeks is, of course, 14 days.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集