【问题标题】:Find out if a time period matches the current time找出时间段是否与当前时间匹配
【发布时间】:2011-04-22 19:57:15
【问题描述】:

我有这样的约会:

 // 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的时间之内?

例如如果

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

$now = 22:07

这将是一场比赛。

编辑: 澄清一下,$time_in_the_future 是一个反复发生的事件,日期在过去或将来。我只需要匹配小时+分钟,而不是日期的其余部分...基本上我只想比较一天中的时间

【问题讨论】:

  • 你不能只获取所有三个time() 值然后检查$start <= $now && $end >= $now 吗?
  • 根据您的示例:start=21:30end=00:30 不是同一天的时间。您应该在此计算中包括天数,或者只是使用下面我的答案中的函数。它不关心日期,但仍然在end<start 的计算中添加+1 day

标签: php datetime date time


【解决方案1】:

函数 inTimeRange()

我想,这在你的情况下会很好用。

  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'));

输出

是的

注意:所有参数的格式必须为HH:MM。您可以传递date("H:i", ...) 作为此函数的参数。

【讨论】:

  • 返回应该是 return ($tn >= $t1) && ($tn
  • @ack 是的,那是错字。谢谢。
【解决方案2】:

您可以只使用time() 部分以秒为单位检索数值,这适合以编程方式使用。

$time_in_the_future_start = time()+60*60*24*7;
$time_in_the_future_end = time()+60*60*24*7+480;

$now = time();

if($now >= $time_in_the_future_start && $time < $time_in_the_future_end) {
    // between times
}

然后以 hh:mm 格式打印值,完成检查后您仍然可以使用 date('H:i')

【讨论】:

  • 是的,但我只想匹配小时:分钟,而不是日、月、年等。这个日期是经常性事件,所以我只需要H:i 就可以了
  • 哦,在那种情况下,我会调查mktime
【解决方案3】:

这是一个比较当天时间的示例。请注意,这不会像您要求的那样比较日期。

这会将时间转换为当天的小时数,以便我们可以将其作为一个数字进行比较。

$time_in_the_future_start = time()+60*60*24*7;
$time_in_the_future_start = date('H', $time_in_the_future_start) + (date('i', $time_in_the_future_start) / 60);
$time_in_the_future_start = time()+60*60*24*7+480;
$time_in_the_future_start = date('H', $time_in_the_future_start) + (date('i', $time_in_the_future_start) / 60);

$now = time();
$now = date('H', $now) + (date('i', $now) / 60);

if($now > $time_in_future_start && $now < $time_in_future_end){
  //withing time span
}else{
  //Not within time span
}

【讨论】:

    【解决方案4】:

    如果您有日期,请在两个时间都使用strtotime() 以获取每个时间的 unix 时间戳。然后查看你想要获取的时间的unix时间戳是否大于_start时间小于_end时间。

    如果它们都已经是时间戳形式,那就更容易了。由于您似乎是从time() 开始的,因此请执行以下操作

    $start = time();
    
    $time_in_the_future_start = $start + 60*60*24*7; 
    $time_in_the_future_end = $start + 60*60*24*7+480;
    
    $time_in_the_future_start_display = date('H:i', $start+60*60*24*7);
    $time_in_the_future_end_display = date('H:i', $start+60*60*24*7+480);
    

    那就做吧

    $curtime = time();
    
    if($curtime < $time_in_the_future_end && $curtime > $time_in_the_future_start)
        // Code here
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多