首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当前时间是否介于两个给定时间之间?

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

Stack Overflow用户
提问于 2013-02-05 01:52:30
回答 11查看 5.3K关注 0票数 16

如果当前时间在餐厅的开放时间内,我会尝试计算

这个问题在Stackoverflow上已经被问了很多,但我还没有找到一个可以解释我所遇到的问题的原因。此外,将很高兴看到一个更好的方式来做这件事的想法。

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

不管怎么说,这就是我所拥有的。请告诉我做这件事的更好方法。

我有这样的时间存储:

代码语言:javascript
复制
$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'
);

这是我现在使用的代码:

代码语言:javascript
复制
// 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) is a WordPress function

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2013-10-20 04:49:20

以下是我的面向对象的解决方案,基于PHP (从5.2版本开始可用)的使用:

代码语言:javascript
复制
<?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对象的openings数组),然后在isOpen方法中通过简单的比较使用该表单,以检查在调用时餐厅是开着还是关着。

您还会注意到,我使用了DateTime:add方法来计算明天的日期,而不是在当前日期时间戳上加上86400 (24*60*60),以避免DST时间转换的问题。

概念验证:

代码语言:javascript
复制
<?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;

?>

它给出了以下结果:

代码语言:javascript
复制
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秒;至少一年不是两次……

票数 3
EN

Stack Overflow用户

发布于 2013-10-22 17:16:32

假设我们有另一个数组,而不是这样的数组,其中包含以下类型的条目:

代码语言:javascript
复制
Array ( [from] => 1382335200 [to] => 1382374800 )

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

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

代码语言:javascript
复制
$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>";

给定一个工作日,格式为montuewed等,并且有两个定义时间范围的字符串,例如8:30am3:15pm,则如下函数将返回相应的时间段,如上所述:

代码语言:javascript
复制
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应该会包装到本周的周一,同样的时间。呼!

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

代码语言:javascript
复制
$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]);
    }
  }

这是a little phpfiddle to demonstrate this

编辑:受到另一个答案中“简洁”讨论的启发,我想我应该给出我的“紧凑”版本。使用完全相同的逻辑,它可以归结为以下内容:

代码语言:javascript
复制
$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数组还可以很好地缓存和重用,这使得相关计算比重新解析原始数据容易得多。

票数 2
EN

Stack Overflow用户

发布于 2013-10-22 18:38:02

这可能不是最有效的,但它应该能很好地解决你手头的问题:

代码语言:javascript
复制
$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'
);

var_dump(is_open($times, strtotime('sun 1am'))); // true

这里是第一个函数,设计简单;它使用一个开放和关闭时间的网格,并确定给定的时间是否与任何一个范围匹配:

代码语言:javascript
复制
function is_open($times, $now)
{
    $today = strtotime('today', $now);

    $grid = get_time_grid($times);
    $today_name = strtolower(date('D', $today));
    $today_seconds = $now - $today;

    foreach ($grid[$today_name] as $range) {
        if ($today_seconds >= $range[0] && $today_seconds < $range[1]) {
            return true;
        }
    }

    return false;
}

这个函数构建实际的网格;如果一个范围结束在它相应的开始之前,它将创建两个范围,一个范围代表被跨越的每一天。

代码语言:javascript
复制
function get_time_grid($times)
{
    static $next_day = array(
        'mon' => 'tue', 'tue' => 'wed', 'wed' => 'thu',
        'thu' => 'fri', 'fri' => 'sat', 'sat' => 'sun',
        'sun' => 'mon'
    );
    static $time_r = '(\d{1,2}(?::\d{2})?(?:am|pm))';

    $today = strtotime('today');
    $grid = [];

    foreach ($times as $key => $schedule) {
        $day_name = substr($key, -3);
        // match all time ranges, skips "closed"
        preg_match_all("/$time_r - $time_r/", $schedule, $slots, PREG_SET_ORDER);
        foreach ($slots as $slot) {
            $from = strtotime($slot[1], $today) - $today;
            $to = strtotime($slot[2], $today) - $today;

            if ($to < $from) { // spans two days
                $grid[$day_name][] = [$from, 86400];
                $grid[$next_day[$day_name]][] = [0, $to];
            } else { // normal range
                $grid[$day_name][] = [$from, $to];
            }
        }
    }

    return $grid;
}

代码中只有几个注释,但我希望您能跟随正在做的事情。如果你需要任何澄清,请告诉我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14692506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档