duanguilin2007 2016-04-08 19:51
浏览 435
已采纳

如何计算用户在PHP中连续登录的次数

I am just getting back into PHP after about 7 years away so am very rusty.

I am wanting to add a feature to my site whereby it tracks the users logins daily and then prints their logins visually on the website. I want it to record consecutive logins and when the user misses a day, return the value to zero.

Any help would be much appreciated!

Cheers!

  • 写回答

2条回答 默认 最新

  • douyun3799 2016-04-08 20:05
    关注

    You should add a consecutiveDayCounter and a lastDayConnected row in your user table.

    In your SELECT query to check if the user and password matches, you should also SELECT these row. Verify if lastDayConnected == than yesterday timestamp.

    Then, do an UPDATE query with today timestamp for the lastDayConnected , and if the check for lastDayConnected == yesterday timestamp, add +1 to consecutiveDayCounter. If not, update it to 0.

    EDIT :

    As asked, here's some example code to get today - yesterday stamp :

    <?php
    // Today
    $today = new DateTime();                 // Create new date object (today date)
    echo $today->format('Y-m-d');            // Format the date 'YYYY-MM-DD'
    
    
    // Yesterday 
    $yesterday = new DateTime();               // Create new date object (today date)
    $yesterday->sub(new DateInterval('P1D'));  // Substract 1 day to it
    echo $yesterday->format('Y-m-d');          //Format the date 'YYYY-MM-DD'
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊