dqq9695 2014-01-30 20:58
浏览 570
已采纳

PHP检查日期和时间是否已超过15分钟

I'm having some troubles calculating if time is past 15 minutes, on a date and time in my database. How can I check this?

I have this in my database (example):

2014-01-30 15:29:31

And then I get the date from PHP:

$date = date("Y-m-d H:i:s");

But how can I check if the time have passed 15 minutes, or if it's a different day?

I have this code so far:

$date = date("Y-m-d H:i:s");
$sql = "SELECT date FROM reset WHERE session_id = '".$sessid."' limit 1";
$result = mysql_query($sql);
$value = mysql_fetch_object($result);
$dbdate = $value->date;

$checkdate = strtotime($dbdate);
if ($checkdate - time() > 15 * 60) {
error_log("15 mins passed");
}
  • 写回答

3条回答 默认 最新

  • dragon456101 2014-01-30 21:04
    关注

    You can parse the date from the database and compare it.

    $dbdate = strtotime($datefromdb);
    if (time() - $dbdate > 15 * 60) {
        // 15 mins has passed
    }
    

    And to compare the date:

    if (date("Y-m-d", $dbdate) != date("Y-m-d")) {
        // different date
    }
    

    I included the year and the month to also notice changes in only them (e.g. 2014-01-01 to 2014-02-01). Use "d" instead of "Y-m-d" to check only the date.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器