了解一段时间是否与当前时间相匹配 [英] Find out if a time period matches the current time

查看:153
本文介绍了了解一段时间是否与当前时间相匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的日期:

 // just a example date
 $time_in_the_future_start = date('H:i', time()+60*60*24*7);
 $time_in_the_future_end = date('H:i', time()+60*60*24*7+480);

 $now = date('H:i', time());

如何查看当前时间是否在 $ time_in_the_future 的时间?

How can I find out if the current time is within $time_in_the_future's time?

例如,如果

$time_in_the_future_start = 21:30;
$time_in_the_future_end = 0:30;

$now = 22:07

这将是一场比赛。

编辑:
澄清, $ time_in_the_future 是一个重复的事件,有日期在过去或将来。我只需要几分钟的时间来匹配,而不是剩下的日期...基本上我只想比较一天中的时间。

edit: to clarify, the $time_in_the_future is a recurring event, with a date in the past or future. I only need the hours+minutes to match, not the rest of the date... Basically I just want to compare the time in a day

推荐答案

函数inTimeRange()



我猜,这可以在你的情况下完美工作。

function inTimeRange()

I guess, this will work perfectly in your case.

  function inTimeRange($time_start, $time_end, $time_needle) {
    $res = false;
    $t1 = strtotime("1970-01-01 {$time_start}:00");
    $t2 = strtotime("1970-01-01 {$time_end}:00");
    $tn = strtotime("1970-01-01 {$time_needle}:00");
    if ($t1 >= $t2) $t2 = strtotime('+1 day', $t2);
    return ($tn => $t1) && ($tn <= $t2); // or return ($tn > $t1) && ($tn < $t2);
    }

  var_dump(inTimeRange('21:30', '00:30', '22:07'));



输出



Output


true

true

注意:所有参数必须格式为 HH:MM 。你可以通过这个函数的参数 date(H:i,...)

Note: All parameters must be in format HH:MM. You can pass date("H:i", ...) for parameters of this function.

这篇关于了解一段时间是否与当前时间相匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆