JBTALKS.CC

标题: 询问: 用PHP做防灌水模式,找出日期与时间相差 [打印本页]

作者: Exm    时间: 2017-4-11 03:23 PM
标题: 询问: 用PHP做防灌水模式,找出日期与时间相差
本帖最后由 Exm 于 2017-5-2 04:16 PM 编辑

我想在我的网站做一个功能是防灌水,
我采用的是当client发一个贴,我都会拿对方的ip和最后发帖时间。
然后开始计算发帖时间,如果发帖时间少过3分钟,否则不能再发

  1. $lastDate = //get from mysql
  2. $nowDate = date('Y-m-d H:i:s', time());//这是我采用日期的格式
  3. if ($lastIp == $currentIP){
  4.         if (($lastDate - $nowDate) <3 ){ //就是这一段我不会
  5.                 //display error
  6.         }
  7. }else{
  8.       //do function
  9. }
复制代码
我要怎样才能计算出日期和时间的相差?

(我不想要用cookie和session的方式来设定防灌水)



已经找到答案
作者: jonathan.goh333    时间: 2017-4-24 08:34 PM
知道unix timestamp么?
直接用那个做加减
没记错的话是 time() 函数
作者: 三岁学抽烟    时间: 2017-5-2 11:50 AM

  1. $lastDate = date('Y-m-d H:i:s', time()-180); //这个是提前3分钟的时间
  2. $nowDate = date('Y-m-d H:i:s', time());//这是我采用日期的格式
  3. $checkDate = (strtotime($nowDate) - strtotime($lastDate));
  4. if ($checkDate <= 180 ) //180 = 3minutes (3*60sec)
  5. {
  6.    echo "Within 3minutes";
  7. }
  8. else
  9. {
  10.     echo "Without 3minutes";
  11. }
复制代码
试试看行的通没?

作者: Exm    时间: 2017-5-2 04:15 PM
本帖最后由 Exm 于 2017-5-2 04:18 PM 编辑
三岁学抽烟 发表于 2017-5-2 11:50 AM
试试看行的通没?


谢谢你,我之前自己研究到了。和你的做法是一样的,谢谢你

  1. $lastDate = date($lastDate); //get from mysql
  2. $nowFormat= date('Y-m-d H:i:s', time());//这是我采用日期的格式

  3. if ($lastIP == $user_ip){ // check ip
  4.         $firstTime=strtotime($lastDate); //become date format
  5.         $lastTime = strtotime($nowFormat); //become date format
  6.         $timeDiff=$lastTime-$firstTime;
  7.                 if ($timeDiff < 180){
  8.                         echo "Within 3minutes";
  9.                 }else{
  10.                         echo "Without 3minutes";
  11.                         }
  12.         }else{
  13.                 echo"ip different";
  14. }
复制代码





欢迎光临 JBTALKS.CC (https://www.jbtalks.cc/) Powered by Discuz! X2.5