gpt4 book ai didi

php - 使用当前时间和以天为键、以连字符分隔的时间范围为值的数组检查企业是否营业

转载 作者:IT王子 更新时间:2023-10-29 00:18:26 25 4
gpt4 key购买 nike

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

这个问题在 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 .

最佳答案

这是我的面向对象的解决方案,基于 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秒;至少一年两次...

关于php - 使用当前时间和以天为键、以连字符分隔的时间范围为值的数组检查企业是否营业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14692506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com