Dre*_*ker 16 php time timestamp
我正在尝试计算当前时间是否在餐馆的营业时间内.
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'
);
Run Code Online (Sandbox Code Playgroud)
这是我现在使用的代码:
// 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';
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 3
这是我的面向对象的解决方案,基于 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;
}
?>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,构造函数构建了一个内部表单(openingsDateTime 对象的数组),然后在方法中使用该表单进行简单比较,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;
?>
Run Code Online (Sandbox Code Playgroud)
给出以下结果:
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
Run Code Online (Sandbox Code Playgroud)
因此,似乎一天并不总是有86400秒;至少一年不会两次...