doulao5916 2019-02-11 16:31
浏览 21
已采纳

两个时间戳之间无法区分

I want return the number of dates which have a difference less than 30 minutes. These are the records stored in the database:

id | last_login
0    2019-02-11 17:22:47
1    0000-00-00 00:00:00
2    2019-02-11 17:22:03
3    0000-00-00 00:00:00
4    0000-00-00 00:00:00

the result of the query should be 2, but I get 1:

$time = strtotime('+30 minutes');

return $this->db->select('COUNT(last_login) AS users_online')
    ->from('login')
    ->where('UNIX_TIMESTAMP(last_login) < ' . $time)
    ->get()->num_rows();
  • 写回答

1条回答 默认 最新

  • 普通网友 2019-02-11 16:41
    关注

    There are two problems with your code here. The first, is you are cutting everything down to 1 record with an implicit group thanks to count() - and then getting the number of records returned via num_rows().

    The second issue is you are getting every single record in the table where the last login is less than 30 minutes in the future... Which will always be every row.

    echo date('H:i') . "
    " . date('H:i', strtotime('+30 minutes'));
    

    Yields:

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

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?