检查自PHP上次更新以来已经过了几天 [英] Check how many days were passed since the last update in PHP

查看:171
本文介绍了检查自PHP上次更新以来已经过了几天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查用户上次进入系统后已经过了几天。我得到最后一次他从mysql表列(datetime)输入。所以我写道:

I am trying to check how many days were passed since the user last entered the system. I get the last time he\she entered from mysql table column (datetime). so I wrote :

$user_last_visit = $user_info['last_updated']; // 2013-08-08 00:00:00

$user_last_visit_str = strtotime($user_last_visit); // 1375912800
$today = strtotime(date('j-m-y')); // 1250114400

$diff = $today - $user_last_visit_str;




  • 其中 $ user_info ['last_updated'] 最后一次他的访问时间是2013-08-08 00:00:00。

  • 在strtotime之后,我得到 $ user_last_visit_str 等于1375912800

  • $ today 的值为9-08-13在strtotime之后,我得到1250114400。

    • Where $user_info['last_updated'] has the last time he\she visited with the value of 2013-08-08 00:00:00.
    • After strtotime I get $user_last_visit_str equals to 1375912800
    • $today has the value of 9-08-13 and after strtotime I get 1250114400.
    • 某些原因 $ diff = $ today - $ user_last_visit_str; 为负而不是以 24 * 60 * 60 * 1000 (一天= 24 * 60 * 60 * 1000 ms)。

      Some reason $diff = $today - $user_last_visit_str; is negative instead of getting a positive value with 24*60*60*1000 (one day = 24*60*60*1000 ms).

      任何想法?

      推荐答案

      p>使用 diff 的简单解决方案:

      A simple solution using diff:

      echo date_create()->diff(date_create($user_last_visit))->days;
      

      如果所有其他失败,只需执行以下操作:

      If all else fails, just do:

      $diff = floor((time() - $user_last_visit_str) / (60 * 60 * 24));
      

      这篇关于检查自PHP上次更新以来已经过了几天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆