如何检查时间是否超过30分钟?

时间:2017-07-27 06:18:01

标签: php

我节省了时间。我想检查一下保存的时间是否比当前时间长30分钟。您可以在代码中看到我到目前为止所做的工作,但它不起作用。

我需要一些修复该代码的帮助。

$current = "08:05";
$to_check = date('h:i');

if($to_check > $current + strtotime('30 minute')){
    echo "greater";
}
else {
  echo "not greater";
}

6 个答案:

答案 0 :(得分:2)

首先,您的$current不是当前时间$to_check,因此您的变量名称具有误导性。

话虽如此,将您的“08:05”存储为Unix时间戳,然后查看两者之间的差异是否大于30 * 60。

答案 1 :(得分:1)


$seconds = 1800; // 30 mins
$time1 = strtotime($db_time) + $seconds;
if(time() >= $time1)
   echo "Time's up!"
else
   echo "time has not expired";

答案 2 :(得分:0)

这应该适合你:

<?php
$current = date('H:i');
$to_check = date('H:i', strtotime('+30 minutes')); 
$to_check = date('H:i', strtotime($record['date'])); //If value is from DB

if($to_check > $current){
    echo "greater";
}
else {
  echo "not greater";
}

答案 3 :(得分:0)

使用时间功能节省时间这将为您提供从1970年开始的时间,例如当前时间为1501137539,30分钟后将为(1501137539 + 30 * 60),因此您只需要检查是否存在差异b / w当前时间和存储时间大于30 * 60,然后半小时结束。

答案 4 :(得分:0)

试试这个代码:

public void setBillDate(Date billDate) {
    this.billDate = Optional
            .ofNullable(billDate)
            .map(Date::getTime)
            .map(Date::new)
            .orElse(null);
}

或者去亲,并且有一个自定义的“秒”,只因为你可以?

function x_seconds($to_check = 'YYYY-mm-dd H:i:s') {
    $to_check = strtotime($to_check);
    $current = time() + 1800; //We add 1800 seconds because it equals to 30 minutes
    return ($current>=$to_check) ? true : false;
}

使用示例:

function x_seconds($to_check = 'YYYY-mm-dd H:i:s', $seconds = 1800) {
    $to_check = strtotime($to_check);
    $current = time() + $seconds; //We add x seconds
    return ($current>=$to_check) ? true : false;
}

答案 5 :(得分:0)

试试这个

$current = strtotime('12:00:00');
$to_check = strtotime(date('H:i:s'));   
$newtime =  round(( $to_check - $current ) /60 );

if($time > 30){    
    echo "greater";
}else {      
    echo "not greater";    
}