当前时间是否介于两个给定时间之间?

2022-08-31 01:19:11

我正在尝试计算当前时间是否在餐厅的营业时间内

这个问题在Stackoverflow上被问了很多,但我还没有找到一个可以解释我所遇到的问题的问题。另外,很高兴看到关于更好方法来做到这一点的想法。

目前,如果当天关闭(在本例中为星期日),或者如果它是“星期六”的凌晨1点(所以从技术上讲,周日早上凌晨1点),它就会中断。我有一种感觉,我必须改变数据的存储方式,以便在午夜之后进行解释,但我正在尝试使用我现在拥有的东西。这是一个问题,因为大多数餐馆将某一天的营业时间列为下午5点至凌晨2点,而不是下午5点至凌晨12点,凌晨12点至凌晨2点。

无论如何,这就是我所拥有的。请告诉我一个更好的方法来做到这一点。

我有时间存储如下:

$times = array(
    'opening_hours_mon' => '9am - 8pm',
    'opening_hours_tue' => '9am - 2am',
    'opening_hours_wed' => '8:30am - 2am',
    'opening_hours_thu' => '5:30pm - 2am',
    'opening_hours_fri' => '8:30am - 11am',
    'opening_hours_sat' => '9am - 3pm, 5pm - 2am',
    'opening_hours_sun' => 'closed'
);

这是我现在使用的代码:

// Get the right key for today
$status = 'open';
$now = (int) current_time( 'timestamp' );
$day = strtolower( date('D', $now) );
$string = 'opening_hours_'.$day;

$times = $meta[$string][0]; // This should be a stirng like '6:00am - 2:00am' or even '6:00am - 11:00am, 1:00pm to 11:00pm'.

// Does it contain a '-', if not assume it's closed.
$pos = strpos($times, '-');
if ($pos === false) {       
    $status = 'closed';
} else {

    // Maybe a day has multiple opening times?
    $seating_times = explode(',', $times);
    foreach( $seating_times as $time ) {

        $chunks = explode('-', $time);
        $open_time = strtotime($chunks[0]);
        $close_time = strtotime($chunks[1]);

        // Calculate if now is between range of open and closed
        if(($open_time <= $now) && ($now <= $close_time)) {
            $status = 'open';
            break;
        } else {
            $status = 'closed';             
        }

    }

}

注意:current_time('timestamp',0)是一个WordPress函数


答案 1

以下是我的面向对象解决方案,基于 PHP DateTime 类的用法(自 5.2 版本起可用):

<?php 

class Restaurant {
    private $cw;
    private $times = array();
    private $openings = array();

    public function __construct(array $times) {
        $this->times = $times;
        $this->setTimes(date("w") ? "this" : "last");
        //print_r($this->openings);       // Debug
    }

    public function setTimes($cw) {
        $this->cw = $cw;
        foreach ($this->times as $key => $val) {
            $t = array();
            $buf = strtok($val, ' -,');
            for ($n = 0; $buf !== FALSE; $n++) {
                try {
                    $d = new DateTime($buf);
                    $d->setTimestamp(strtotime(substr($key, -3)." {$this->cw} week {$buf}"));
                    if ($n && ($d < $t[$n-1])) {
                        $d->add(new DateInterval('P1D'));
                    }
                    $t[] = $d;
                } catch (Exception $e) {
                    break;
                }
                $buf = strtok(' -,');
            }
            if ($n % 2) {
                throw new Exception("Invalid opening time: {$val}");
            } else {
                $this->openings[substr($key, -3)] = $t;
            }
        }
    }

    public function isOpen() {
        $cw = date("w") ? "this" : "last";
        if ($cw != $this->cw) {
            $this->setTimes($cw);
        }
        $d = new DateTime('now');
        foreach ($this->openings as $wd => $t) {
            $n = count($t);
            for ($i = 0; $i < $n; $i += 2) {
                if (($d >= $t[$i]) && ($d <= $t[$i+1])) {
                    return(TRUE);
                }
            }
        }
        return(FALSE);
    }
}

$times = array(
    'opening_hours_mon' => '9am - 8pm',
    'opening_hours_tue' => '9am - 2am',
    'opening_hours_wed' => '8:30am - 2am',
    'opening_hours_thu' => '9am - 3pm',
    'opening_hours_fri' => '8:30am - 11am',
    'opening_hours_sat' => '9am - 3pm, 5pm - 2am',
    'opening_hours_sun' => 'closed'
);

try {
    $r = new Restaurant($times);
    $status = $r->isOpen() ? 'open' : 'closed';
    echo "status=".$status.PHP_EOL;
} catch (Exception $e) {
    echo $e->getMessage().PHP_EOL;
}

?>

如您所见,构造函数构建一个内部窗体(DateTime 对象数组),然后将其与方法中的简单比较一起使用,以检查在调用时餐厅是打开还是关闭。openingsisOpen

您还会注意到,我已使用 DateTime:add 方法来计算明天的日期,而不是将 86400 (24*60*60) 添加到当前日期时间戳,以避免 DST 时移问题。
概念验证:

<?php

ini_set("date.timezone", "Europe/Rome");
echo "date.timezone = ".ini_get("date.timezone").PHP_EOL;

$d1 = strtotime("2013-10-27 00:00:00");
$d2 = strtotime("2013-10-28 00:00:00");
// Expected: 86400, Result: 90000
echo "Test #1: ".($d2 - $d1).PHP_EOL;
// Expected: 2013-10-28 00:00:00, Result: 2013-10-27 23:00:00
echo "Test #2: ".date("Y-m-d H:i:s", $d1 + 86400).PHP_EOL;

$d1 = strtotime("2014-03-30 00:00:00");
$d2 = strtotime("2014-03-31 00:00:00");
// Expected: 86400, Result: 82800
echo "Test #3: ".($d2 - $d1).PHP_EOL;
// Expected: 2014-03-30 00:00:00, Result: 2014-03-29 23:00:00
echo "Test #4: ".date("Y-m-d H:i:s", $d2 - 86400).PHP_EOL;

?>

这给出了以下结果:

date.timezone = Europe/Rome
Test #1: 90000
Test #2: 2013-10-27 23:00:00
Test #3: 82800
Test #4: 2014-03-29 23:00:00

因此,似乎一天并不总是有86400秒;至少不是一年两次...


答案 2

假设我们没有这样的数组,而是另一个具有以下类型的条目:

Array ( [from] => 1382335200 [to] => 1382374800 )

和 值是时间戳,通过将数组的信息投影到当前(运行)周来计算。fromto

然后,为了检查餐厅现在是否开放,我们必须做一些简单的事情,例如:

$slots=..... /* calculate time slots array */
$status='closed';
$rightnow=time();
foreach($slots as $slot)
  if($rightnow<=$slot['to'])
    {
    if($rightnow>=$slot['from']) $status='open';
    break;
    }
echo "The restaurant is <strong>$status</strong> right now<br>";

给定一个工作日,以 、 等和两个定义时间范围的字符串(例如 和 )的形式,以下函数将返回相应的时间段,如上所述:montuewed8:30am3:15pm

function get_time_slot($weekday,$fromtime,$totime)
  {
  $from_ts=strtotime("this week $weekday $fromtime");
  $to_ts=strtotime("this week $weekday $totime");
  if($to_ts<$from_ts)
    {
    $to_ts=strtotime("this week $weekday +1 day $totime");
    if($to_ts>strtotime("next week midnight")) 
      $to_ts=strtotime("this week mon $totime");
    }
  return array('from'=>$from_ts,'to'=>$to_ts);
  }

strtotime()可以创造奇迹,对吧?请注意,如果时间段的结束时间早于开始时间,我们假设它指的是第二天,然后我们这样重新计算它。

编辑:起初,我天真地以为我会通过增加一天的几秒钟来纠正它。这并不完全准确,因为操作时间戳不会保留 DST 信息。因此,如果一个时间段包括白班(午夜)和一个DST班次,它将在一小时内给出不准确的结果。用相同的论点加上一天,再次使用所有内容,可以纠正它。strtotime()

yaedit:修复了另一个错误(希望是最后一个错误):当一家餐厅在周日营业到午夜之后时,应该结束到本周的星期一,同一时间。唷!$to_time

现在,为了转换您的数组,您需要执行以下操作:

$slots=array();
foreach($times as $key=>$entry)
  {
  list(,,$dow)=explode('_',$key);
  foreach(explode(',',$entry) as $d)
    {
    $arr=explode('-',$d);
    if(count($arr)==2) $slots[]=get_time_slot($dow,$arr[0],$arr[1]);
    }
  }

这里有一个小的phpfiddle来演示这一点


编辑:在另一个答案的“简洁”讨论的激励下,我想我会给出我的“紧凑”版本。使用完全相同的逻辑,它归结为以下内容:
$status='closed';
$rightnow=time();
foreach($times as $key=>$entry)
  {
  list(,,$dow)=explode('_',$key);
  foreach(explode(',',$entry) as $d)
    if(count($arr=explode('-',$d))==2)
      {
      $from_ts=strtotime("this week $dow {$arr[0]}");
      $to_ts=strtotime("this week $dow {$arr[1]}");
      if($to_ts<$from_ts) $to_ts=strtotime("this week $dow +1 day {$arr[1]}");
        {
        $to_ts=strtotime("this week $dow +1 day {$arr[1]}");
        if($to_ts>strtotime("next week midnight")) 
          $to_ts=strtotime("this week mon {$arr[1]}");
        }
      if($rightnow<=$to_ts)
        {
        if($rightnow>=$from_ts) $status='open';
        break 2; // break both loops
        }
      }
  }
echo "<hr>The restaurant is <strong>$status</strong> right now<br>";

但是,我自己仍然更喜欢原始版本。除了具有函数的明显好处之外,数组还可以很好地缓存和重用,这使得相关计算比再次解析原始数据容易得多。$slots


推荐