【问题标题】:Is the current time between two given times?当前时间是否在两个给定时间之间?
【发布时间】:2013-01-19 11:49:48
【问题描述】:

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

这个问题在 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) is a WordPress function.

【问题讨论】:

  • 对时间数组的存储方式是否灵活?
  • 最好不要,因为我已经按照描述存储了 50 多家餐厅。但也许唯一的方法是重新格式化存储的时间......
  • @DrewBaker 更有理由更改存储格式并“手动”更新当前 50 多个数据...
  • 如果餐厅将其时间指定为周五晚上 8 点到凌晨 1 点,那么您可以在内部将其转换为周五晚上 8 点到周六凌晨 1 点,必要时将其拆分为两部分。
  • 我没有看到你对已经给出的答案有太多的活动......你在审查它们吗?

标签: php time timestamp


【解决方案1】:

这是我的面向对象的解决方案,基于 PHP DateTime class 的使用(从 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 对象的openings 数组),然后将其与isOpen 方法中的简单比较一起使用,以检查是否在调用餐厅时已打开或关闭。

您还会注意到,我使用 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 秒;一年至少两次……

【讨论】:

  • 提前 1 天计算的结果非常有趣。关于了解 DST 的一个很好的说明。
  • 这对我来说似乎是最好的答案,其次是 Uours 的答案。我喜欢这个,因为它很紧凑,尽管它的复杂程度可能高于某些程序员(但我不能反对它)。有人看到这有什么问题吗?如果时区设置正确,似乎对我来说效果很好。
  • 似乎不适用于周一下午 5 点至凌晨 2 点这样的开放时间。它会说周二凌晨 1 点关门,但应该开门。
  • @DanielP 你说得对;谢谢你指出。我稍微修改了代码,所以现在应该修复了。
  • 一开始并不确定,但如果您在周日下午 5 点至凌晨 2 点,而现在是周一凌晨 1 点,它也会说关闭,而它应该是开放的。我也想知道如果该构造被称为星期日(假设在午夜之前)并且在星期一稍后调用“isOpen”会发生什么。我认为,如果由于某种原因发生了变化,那么在再次调用构造的地方包含一些内容可能是明智的。
【解决方案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>";

给定一个工作日,形式为montuewed等和两个定义时间范围的字符串,例如8: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]);
    }
  }

这里是a little phpfiddle to demonstrate this


编辑:受另一个答案中“简洁”讨论的启发,我想我会给出我的“紧凑”版本。使用完全相同的逻辑,可以归结为以下内容:
$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 数组还可以很好地被缓存和重复使用,这使得相关计算比重新解析原始数据容易得多。

【讨论】:

  • 很有趣,但我无法更改数据的格式。或者我不喜欢,因为它将由用户生成或从 yelp 导入。
  • @DrewBaker:是的,这就是整个想法。您转换数据仅用于计算,在一个单独的数组$slots 中。除此之外,您的数据保持原样。这正是为了以您的方式接收数据,而没有任何建议更改您现有的应用程序逻辑。
  • 我认为你做了很多不必要的工作 - 你检查了所有的音调,但我们特别有趣的一天
  • @CreatoR:“很多”与什么相比?如果这就是你的意思,我不会检查所有工作日。直到我仍然有机会使$status 等于open。如果您可以将其称为开销,那么开销对于此类数据来说是微不足道的。
  • @geomagas 我不称其为开销数据,但您的脚本将检查周日的所有工作日,而不是周六。您的“机会”将仅在星期一,并在开始时结束检查。想象一下,如果数据库包含大量餐馆,而周日对您的脚本来说真是地狱:)当我们有特定的日子时,我只是称其为不必要的操作。
【解决方案3】:

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

$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

这是第一个功能,设计简单;它使用开盘和关盘时间网格,并确定给定时间是否与任何范围匹配:

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

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

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

代码中只有几个cmets,但我希望你能关注正在做的事情。如果您需要任何说明,请告诉我。

【讨论】:

    【解决方案4】:

    我过去做过类似的事情,但采取了完全不同的方法。我将营业时间存储在单独的表格中。

    CREATE TABLE `Openinghours`
    (
        `OpeninghoursID` int, // serial
        `RestaurantID` int, // foreign key
        `Dayofweek` int, // day of week : 0 (for Sunday) through 6 (for Saturday)
        `Opentime` int, // time of day when restaurant opens (in seconds)
        `Closetime` int // time of day when restaurant closes (in seconds)
    );
    

    如果餐厅每天有多个营业时间,您只需添加 2 条记录(或需要更多 ID)。使用这样的表的好处是您可以简单地查询以查看哪些餐厅营业。

    $day = date('w');
    $now = time()-strtotime("00:00");
    $query = "Select `RestaurantID` from `Openinghours` where `Dayofweek` = ".$day." and `Opentime` <= ".$now." and `Closetime` > ".$now;
    

    使用这种系统的另一个好处是,您可以调整查询以获得不同的结果,例如:哪些餐厅现在营业并至少再营业一个小时(没有必要在关闭前几分钟去餐厅)

    $day = date('w');
    $now = time()-strtotime("00:00");
    $query = "Select `RestaurantID` from `Openinghours` where `Dayofweek` = ".$day." and `Opentime` <= ".$now." and `Closetime` > ".($now+3600);
    

    当然,它需要重新格式化您当前的数据,但它带来了不错的功能。

    【讨论】:

    • 对于能够使用您所描述的表格的人来说,这是一个很好的建议。
    【解决方案5】:

    这是另一个无需重新格式化数据的解决方案。

    $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'
    );
    
    function compileHours($times, $timestamp) {
        $times = $times['opening_hours_'.strtolower(date('D',$timestamp))];
        if(!strpos($times, '-')) return array();
        $hours = explode(",", $times);
        $hours = array_map('explode', array_pad(array(),count($hours),'-'), $hours);
        $hours = array_map('array_map', array_pad(array(),count($hours),'strtotime'), $hours, array_pad(array(),count($hours),array_pad(array(),2,$timestamp)));
        end($hours);
        if ($hours[key($hours)][0] > $hours[key($hours)][1]) $hours[key($hours)][1] = strtotime('+1 day', $hours[key($hours)][1]);
        return $hours;
    }
    
    function isOpen($now, $times) {
        $open = 0; // time until closing in seconds or 0 if closed
        // merge opening hours of today and the day before
        $hours = array_merge(compileHours($times, strtotime('yesterday',$now)),compileHours($times, $now)); 
    
        foreach ($hours as $h) {
            if ($now >= $h[0] and $now < $h[1]) {
                $open = $h[1] - $now;
                return $open;
            } 
        }
        return $open;
    }
    
    $now = strtotime('7:59pm');
    $open = isOpen($now, $times);
    
    if ($open == 0) {
        echo "Is closed";
    } else {
        echo "Is open. Will close in ".ceil($open/60)." minutes";
    }
    
    ?>
    

    我进行了几次测试,它似乎按预期工作,考虑了我能想到的所有方面。如果你发现这个有问题,请告诉我。 我知道这种方法看起来有点讨厌,但我只想使用简单的函数(除了 array_map 的棘手部分)并尽可能短。

    【讨论】:

    • 这看起来确实很简洁。我会测试一下。
    • 出于测试目的,您可以调整$now = time(); 行,如果您想要数组的人类可读输出,只需在末尾添加foreach ($hours as $h) { echo date("d H:i:s",$h[0]).' - '.date("d H:i:s",$h[1]).'&lt;br /&gt;'; }
    • 嘿@Daniel P 你能包含一些关于如何调用这个函数的代码吗?我不确定如何从中获得“打开”或“关闭”状态。
    • 稍微修改了代码,使功能更加明显(我希望如此)。函数isOpen的返回值在关闭时等于0,在打开时它会反映距离关闭还剩多少秒,这可以方便地让人们意识到它即将关闭。
    【解决方案6】:

    周计时数组

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

    用法示例:

    ini_set( "date.timezone", "Pacific/Auckland" ); // Make sure correct timezone is set
    
    echo ( isOpen( $times ) ? 'Open' : 'Closed' );    
    
    echo ( isOpen( $times ,"10am" ) ? 'Open' : 'Closed' );
    

    函数定义:

    /*
     *  First parameter : Weeks timings as array
     *  Second parameter : Time to check as string
     *  Return value : boolean
     */
    function isOpen( $times ,$timeToCheck = 'now' )
    {
        $timeToCheckAsUnixTimestamp = strtotime( $timeToCheck );
    
        $yesterdayTimes = $todayTimes = '';
        //  Find yesterday's times and today's times
        foreach( $times as $day => $timeRange )
        {
            $yesterdayTimes = ( stripos( $day ,date( "D" ,time() - 60 * 60 * 24 ) ) !== false ? $timeRange : $yesterdayTimes );
            $todayTimes = ( stripos( $day ,date( "D" ) ) !== false ? $timeRange : $todayTimes );
        }
        //  Handle closed
        if( strcasecmp( $todayTimes ,'closed' ) == 0 ) return false;
        if( strcasecmp( $yesterdayTimes ,'closed' ) == 0 ) $yesterdayTimes = '12am - 12am';
        //  Process and check with yesterday's timings
        foreach( explode( ',' ,$yesterdayTimes ) as $timeRanges )
        {
            list( $from ,$to ) = explode( '-' ,$timeRanges );
            list( $fromAsUnixTimestamp ,$toAsUnixTimestamp ) = array( strtotime( $from .' previous day' ) ,strtotime( $to .' previous day'  ) );
            $toAsUnixTimestamp = ( $toAsUnixTimestamp < $fromAsUnixTimestamp ? strtotime( $to ) : $toAsUnixTimestamp );
            if( $fromAsUnixTimestamp <= $timeToCheckAsUnixTimestamp and $timeToCheckAsUnixTimestamp <= $toAsUnixTimestamp ) return true;
        }
        //  Process and check with today's timings
        foreach( explode( ',' ,$todayTimes ) as $timeRanges )
        {
            list( $from ,$to ) = explode( '-' ,$timeRanges );
            list( $fromAsUnixTimestamp ,$toAsUnixTimestamp ) = array( strtotime( $from ) ,strtotime( $to ) );
            $toAsUnixTimestamp = ( $toAsUnixTimestamp < $fromAsUnixTimestamp ? strtotime( $to .' next day' ) : $toAsUnixTimestamp );
            if( $fromAsUnixTimestamp <= $timeToCheckAsUnixTimestamp and $timeToCheckAsUnixTimestamp <= $toAsUnixTimestamp ) return true;
        }
        return false;
    }
    

    【讨论】:

    • 这个答案非常复杂,我担心与这里的其他一些解决方案相比,它很难维护。
    • 我已经对它进行了一定程度的测试,似乎可以完美地工作,但可以肯定它很长而且看起来很复杂。可能是最长的解决方案!
    • 更新了更短、更简单的版本。
    • 刚刚在一家列为下午 5:30 - 凌晨 2 点的餐厅累了(晚上 8:40),但它错误地显示为关闭。
    • 嗯,它对我有用。以防万一,您是否将时区修改为您的时区?
    【解决方案7】:

    如果您为此使用数据库,为什么不为此使用 datetime

    示例:

    sunday 14:28, saturday 1:28
    

    您可以拆分这两部分并在字符串时间(第 2 部分)中比较它们。您可以使用 strtotime 将字符串时间转换为时间戳并进行比较。

    示例:

    $date = "sunday 14:28"; 
    echo $stamp = strtotime($date);
    

    输出:

    1360492200
    

    喜欢这段代码:

    $Time="sunday  14:28 , saturday 1:28";
    $tA=explode(",",$Time);
    $start=strtotime($tA[0]);
    $end=strtotime($tA[1]);
    $now=time();
    if($now>$start and $now<$end){
       echo "is open";
    }else{
       echo "is close";
    }
    

    但是您在更新它们时遇到问题,您可以这样做。

    【讨论】:

      【解决方案8】:

      不改变时间存储格式的解决方案

      <?php
          $times = array(
              'opening_hours_mon' => '9am - 8pm',
              'opening_hours_tue' => '5pm - 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'
          );
      
          //$time_go = '13:00';
          $time_go = date('H:i');
      
          //$day_go = 1; //monday
          $day_go = (date('N') - 1);
      
          if(Are_they_open($time_go, $day_go, $times)){
              echo 'jep';
          }
          else{
              echo 'nope';
          }
      
          function Are_they_open($time_go, $day_go, $times){
              // the magic
              $times = array_values($times);
              $day_go = explode(',', $times[$day_go]);
              $time_go = hh_mm_toSeconds($time_go);
      
              foreach($day_go as $time){
      
                  if((!$time) || ($time == 'closed')){
                      return false;
                  }
      
                  $time = explode(' - ', $time);
                  $time_open = hh_mm_toSeconds(date('H:i', strtotime($time[0])));
                  $time_close = hh_mm_toSeconds(date('H:i', strtotime($time[1])));
      
                  if(($time_go > $time_open) && ($time_go < $time_close)){
                      return true;
                  }
                  elseif (($time_open > $time_close) || ($time_go > $time_open)){
                      return true;
                  }
      
              }
      
              return false;
          }
      
          function hh_mm_toSeconds($str_time){
              sscanf($str_time, "%d:%d", $hours, $minutes);
              return ($hours * 3600) + ($minutes * 60);
          }
      ?>
      

      更改时间格式的解决方案

      $times = array(
          1 => array(
              array('07:00', '17:00')
          ),
          2 => array(
              array('07:00', '14:30'),
              array('15:00', '20:00')
          ),
          3 => array(
              array('07:00', '17:00')
          ),
          4 => false, //closed
          5 => array(
              array('07:00', '17:00'),
              array('20:00', '24:00')
          ),
          6 => array(
              array('00:00', '03:00'),
              array('07:00', '17:00'),
              array('20:00', '24:00')
          ),
          7 => array(
              array('00:00', '03:00')
          ),
      );
      

      【讨论】:

      • 规范化、概念、推理、逻辑或者你想说的。如果您不同意我的观点,请提出一些论据。
      • OP 提出了一个简单明了的问题,与大量问题不同。如果认为这个问题不符合你的答案,必须想出比“你的算法错了,因为我不知道'不喜欢你的数据格式".
      • @sirwilliam 你试图强迫用户像程序员一样思考。 OP已经说过改变数据格式并不理想。也可能输入开放时间,因为它们将显示给客户(可能由非程序员),因此您不能期望该用户输入“09:00:00 - 11:59:59”或其他什么,因为它不适合您的代码。数据格式不理想,我们都知道,但您的解决方案应该适合手头的问题,而不是相反。
      • @BadHorse 好吧,如果我必须制作一个类似的系统,我会让用户填写 11:30 到 1:45 然后将其重新格式化为第二天 00 点的 11:30 到 24:00: 00 到 1:45。有时我们必须改变我们的思维方式,以提高工作流程的效率。但那只是我的个人意见。您是否真的测试过我的解决方案,然后我的意思是我为 DrewBaker 编写的代码。它有效。
      【解决方案9】:

      这是我的解决方案:

      输入数据:

      $meta = 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'
      

      );

      current_time('timestamp')(如作者所说)WordPress 中time() 的模拟
      及解决方案:

          $now = (int) current_time( 'timestamp' );
          $day = strtolower(date('D', $now));
          $yesterday = strtolower(date('D', strtotime('-1 day')));
          $days = array(
              'yesterday' => $meta['opening_hours_'.$yesterday],
              'today' => $meta['opening_hours_'.$day],
          );
          $status = false;
          foreach($days as $when=>$times)
          {
              $parts = explode(',',$times);
              foreach($parts as $p)
              {
                  if($p == 'closed')
                      break;
                  else{
                      list($b,$e) = explode('-',$p);
                      $b = strtotime("$when $b");
                      $e = strtotime("$when $e");
                      if($b > $e)
                          $e += strtotime("$when $e +1 day");;
                      if($b <= $now && $now <= $e)
                      {
                          $status =true;
                          break;
                      }
                  }
              }
          }
      

      用于测试:
      您可以将前 3 行更改为以下内容:

      $now = (int) strtotime('today 3:00am');
      $day = strtolower(date('D', $now));
      $yesterday = strtolower(date('D', strtotime('yesterday 3:00am')));
      

      【讨论】:

      • 这个答案似乎比其他答案简洁得多。有人看到这有什么问题吗?
      • 我会测试它,它的工作原理:) 你可以试试这样:你可以设置自定义$yesterday = strtolower(date('D', strtotime('-1 day')));并插入代码$now = strtotime('-2 days 6:00pm');手动设置当前时间。
      • @DrewBaker 除非我遗漏了什么,否则代码不起作用。 current_time 是一个未定义的函数,$meta 是一个未定义的数组,$days 是空的,它当前告诉我它应该在它应该打开的时候关闭。不确定 CreatoR 是否忘记包含更多代码?
      • $meta = $times from author's post, current_time - 是 Wordpress 的功能。我更新帖子并解释你如何测试它
      • @DrewBaker:是的,一个小问题:添加 24*3600 秒并不能准确地呈现到正确的时间戳,在包含午夜 的时隙的极少数情况下夏令时班次。我自己刚刚弄清楚了这一点,并相应地编辑了我的答案——希望这对每个人都有帮助!
      【解决方案10】:

      将您的数组作为参数传递给此函数,您将在当前时间为打开和关闭为 false。这是简单直接的功能。我只检查今天的开放时间,如果有必要的话,我只检查昨天的开放时间,而无需在整个工作日中进行不必要的循环。也许它可以改进一点,但它的工作原理并不复杂。

      function isOpen($times) {
          $times = array_values($times); //we will use numeric indexes
          $now = new DateTime();
          $day = $now->format('N'); 
          $day--; //days are counted 1 to 7 so we decrement it to match indexes
          $period = $times[$day];
          if($period!='closed') {
              $opening = explode('-', $period);
              $open = new DateTime($opening[0]);
              $close = new DateTime($opening[1]);
              if($close<$open) {
                  //it means today we close after midnight, it is tomorrow
                  $close->add(new DateInterval('P1D'));
              }
              if($open<$now && $now<$close) {
                  //we are open
                  return true;
              }
          }
          if($period=='closed' || $now<$open) {
              //now we check if we still open since yesterday
              $day = $day==0 ? 6 : $day-1;
              $period = $times[$day];
              if($period=='closed') return false;
              $opening = explode(' - ', $period);
              $open = new DateTime($opening[0]);
              $close = new DateTime($opening[1]);
              if($close<$open) {
              //it means yesterday we closed after midnight
                  if($now<$close) {
                      //we are before closing time
                      return true;
                  }
              }
          }
          return false;
      }
      

      【讨论】:

        【解决方案11】:

        也许我没有完全理解这一点,但是通过一个基本的循环,也许这可以工作:

        if( ($date('D') == "3" ) )
        {
            $open = "8";
            $close = "17";
            $now = time();
        
            if( ($now > $open) and ($now < $close) )
            {
                echo "open";
            }
            else
            {
                echo "closed";
            }
        }
        

        也许不是最好的解决方法,因为这不包括假期等,并且需要一些条件语句,但我认为这可以工作。嗯,不是最佳的,但一直对我有用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-29
          • 1970-01-01
          • 2018-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多