PHP存储旧值并与新值进行比较

时间:2016-08-21 15:19:45

标签: php arrays session

我有一个每X分钟刷新一次的网页,并显示数组中的一些信息。

Name    Value
-------------
Mark     546
Donald   312

我的问题是,暂时存储旧值并将其与页面刷新之间的新值进行比较的最简单方法是什么(见下文)?它可以是每个用户只是在访问页面时。 Php会话和cookie可能吗?

Name    Value Change
--------------------
Mark     559   +13
Donald   233   -79

1 个答案:

答案 0 :(得分:0)

所以你可以轻松地使用会话

//structure in sessions could be like this
$_SESSION['last'] = $data; // all the data you echo
$_SESSION['last']['timestamp'] = $time; //timestamp when you fetched this data
$_SESSION['previous'] = $old_data; // store the last data

// each time last data is older than 5 min, refresh
if (time() - $_SESSION['last']['timestamp'] > 5 * 60){
  //store current data as previous
  $_SESSION['previous'] = $_SESSION['last'];
  //set new data in $data
  $_SESSION['last'] = $data; // set all your new data
  $_SESSION['last']['timestamp'] = time(); //set timestamp
}