如何识别用户更新后的时间


How can I identify the time after user update?

如何识别用户在我的MySQL数据库中更新其帐户后经过的时间?我的MySQL表中有一个时间戳(用于存储用户更新时间),那么现在如何使用PHP识别上次用户更新经过的时间?

例如:

用户上次更新时间: 2012-04-08 00:20:00;

现在: 2012-04-08 00:40:00;

自上次更新以来经过的时间:20(分钟) ←我需要使用 PHP

如果您有数据

$last_update_time = '2012-04-08 00:20:00';
$now = time(); //this gets you the seconds since epoch

你可以做

$last_update_since_epoch = strtotime($last_update_time); //this converts the string to the seconds since epoch

嗡......现在,由于您在两个变量上都有秒数

$seconds_passed = $now - $last_update_since_epoch;

现在,您可以执行$seconds_passed / 60来获取自上次更新以来经过的分钟数。

希望这对;)有所帮助

在伪代码中:

$get_user_time = mysql_query("SELECT timestamp FROM table");
$row = mysql_fetch_array($get_user_time);
$user_time = $row['timestamp'];  //This is the last update time for the user
$now_unformatted = date('m/d/Y h:i:s a', time());
$now = date("m/d/y g:i A", $now_unformatted); // This is the current time
$difference = $now->diff($user_time); // This is the difference
echo $difference;

diff() 在>= PHP 5.3 中受支持。否则,您可以执行以下操作:

$difference = time() - $user_time;
$secs=time()-strtotime($timestamp);

会在更新之前给你几秒钟,然后你可以把这个秒转换成你需要的时间格式,比如 echo $secs/60." minutes ago";

为什么不使用mysql

select TIMEDIFF(NOW(),db_timestamp) as time_past