Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查时间是否超过30分钟?_Php - Fatal编程技术网

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

Php 如何检查时间是否超过30分钟?,php,Php,我节省了一点时间。我想检查保存的时间是否比当前时间多30分钟。您可以在代码中看到我到目前为止所做的工作,但它不起作用 我需要一些帮助来修复代码 $current = "08:05"; $to_check = date('h:i'); if($to_check > $current + strtotime('30 minute')){ echo "greater"; } else { echo "not greater"; } 首先,您的$current不是当前时间,$to\

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

我需要一些帮助来修复代码

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

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

首先,您的
$current
不是当前时间,
$to\u check
是当前时间,因此您的变量名具有误导性


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

首先,您的
$current
不是当前时间,
$to\u check
是,因此您的变量名具有误导性


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

这应该适用于您:

<?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";
}

这应该适合您:

<?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";
}

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

使用时间功能节省时间这将为您提供1970年以来的秒数,例如,当前时间为1501137539,30分钟后为(1501137539+30*60)因此,您只需检查当前时间和存储时间的差值是否大于30*60,那么半个小时就结束了。

试试这个片段:

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;
}
$date = date('Y-m-d H:i:s', '2017-07-27 05:00');
if(x_seconds($date)):
    print "Is within 30 minutes.";
else:
    print "IS NOT within 30 minutes";
endif;
或者去专业,并有一个自定义的“秒自”,仅仅因为你可以

用法示例:

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;
}
$date = date('Y-m-d H:i:s', '2017-07-27 05:00');
if(x_seconds($date)):
    print "Is within 30 minutes.";
else:
    print "IS NOT within 30 minutes";
endif;

试试这个片段:

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;
}
$date = date('Y-m-d H:i:s', '2017-07-27 05:00');
if(x_seconds($date)):
    print "Is within 30 minutes.";
else:
    print "IS NOT within 30 minutes";
endif;
或者去专业,并有一个自定义的“秒自”,仅仅因为你可以

用法示例:

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;
}
$date = date('Y-m-d H:i:s', '2017-07-27 05:00');
if(x_seconds($date)):
    print "Is within 30 minutes.";
else:
    print "IS NOT within 30 minutes";
endif;

试试这个来解决这个问题

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

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

试试这个来解决这个问题

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

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

查看您的比较单位查看您的比较单位我认为
$to\u check
是从数据库(或任何形式的持久性)加载的,而不是使用
date()创建的
@FabriceKabongo EditedI认为
$to\u check
是从数据库(或任何形式的持久性)加载的,而不是使用
date()创建的
@FabriceKabongo已编辑