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条)

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器