我正在尝试计算如果当前时间在餐馆的营业时间内。
这个问题在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';
}
}
}
答案 0 :(得分:3)
这是我面向对象的解决方案,基于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)添加到当前日期时间戳,以避免{{3}出现问题时间的变化
概念证明:
<?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 :(得分:2)
假设我们使用以下类型的条目而不是这样的数组:
Array ( [from] => 1382335200 [to] => 1382374800 )
from
和to
值是时间戳,通过将数组的信息投影到当前(正在运行)周来计算。
然后,为了检查餐厅现在是否开放,我们必须做一些简单的事情:
$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>";
给定工作日,以mon
,tue
,wed
等形式和两个定义时间范围的字符串,例如8:30am
和{{1} },以下函数将返回相应的时隙,如上所述:
3: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);
}
可以创造奇迹,是吧?请注意,如果时间段的结尾早于开头,我们假设它指的是第二天,我们会重新计算它。
编辑:起初,我天真地以为我会通过添加一天的秒数来纠正它。这不完全准确,因为操纵时间戳不会保留DST信息。因此,如果时间段包括白班(午夜)和也是DST班次,则会在一小时内给出不准确的结果。 1}}再一次使用相同的参数加上一天,可以直接设置它。
yaEDIT:修复了另一个错误(希望是最后一个错误):当一家餐馆在星期日营业至午夜之后,strtotime()
应该在本周的同一时间回归。 呼<!/ em>的
现在,为了转换你的数组,你需要做:
strtotime()
这是a little phpfiddle to demonstrate this。
<小时/> 编辑:在另一个答案的“简洁”讨论的激励下,我想我会给出我的“紧凑”版本。使用完全相同的逻辑,可归结为以下内容:
$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]);
}
}
数组可以很好地被缓存和重用,使得相关计算比重新解析原始数据更容易。
答案 2 :(得分:1)
这可能不是最有效的,但它应该可以很好地解决您手头的问题:
$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;
}
代码中只有一些注释,但我希望你能按照正在进行的操作。如果您需要任何澄清,请与我们联系。
答案 3 :(得分:1)
周定时数组
$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;
}
答案 4 :(得分:1)
我过去做过类似的事情但采取了完全不同的方法。我将开放时间存放在一张单独的表格中。
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 :(得分:1)
这是另一种解决方案,无需重新格式化您的数据。
$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
的棘手部分)并保持尽可能短。
答案 6 :(得分:0)
如果你使用数据库,为什么你没有使用 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";
}
但是你有更新它们的问题,你可以这样做。
答案 7 :(得分:0)
这是我的解决方案:
输入数据:
$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; } } } }
FOR TESTING:
你可以改变前3行:
$now = (int) strtotime('today 3:00am'); $day = strtolower(date('D', $now)); $yesterday = strtolower(date('D', strtotime('yesterday 3:00am')));
答案 8 :(得分:0)
无需更改时间存储格式的解决方案
<?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')
),
);
答案 9 :(得分:0)
将您的数组作为参数传递给此函数,对于当前时间关闭,您将获得open和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;
}
答案 10 :(得分:0)
也许我不完全理解这一点,但是经历一个基本的循环,也许这可能有效:
if( ($date('D') == "3" ) )
{
$open = "8";
$close = "17";
$now = time();
if( ($now > $open) and ($now < $close) )
{
echo "open";
}
else
{
echo "closed";
}
}
也许不是绕过它的最佳方式,因为这不包括假期等,并且需要一些条件语句,但我认为这可行。嗯,不是最优的,但总是为我工作。