简体   繁体   中英

how to check if time is more than 30minute?

I have saved a time. I would like to check whether the saved time is 30 minutes greater than the current time. You can see in the code what I have done so far but it doesn't work.

I would need some help fixing that code.

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

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

First, your $current isn't the current time, $to_check is, so your variable names are misleading.

That being said, store your "08:05" as a Unix timestamp then see if the difference between the two is greater than 30 * 60.

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

This should work for you:

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

使用 time 函数节省时间,这将为您提供自 1970 年以来的秒数,例如当前时间为 1501137539,30 分钟后为 (1501137539 + 30*60),因此您只需要检查当前时间和存储时间大于 30*60 则半小时结束。

Try this snippet:

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

Or go pro, and have a custom "seconds since", just because you can?

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

Example usage:

$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;

Try this to solve the issue

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM