我有一系列营业时间,我正试图找出下一家商店何时开业的方法。
我的数组看起来像
$storeSchedule = [
'Sun' => ['12:00 AM' => '01:00 AM'],
'Mon' => ['09:00 AM' => '12:00 AM'],
'Tue' => ['09:00 AM' => '12:00 AM'],
'Wed' => ['09:00 AM' => '12:00 AM'],
'Thu' => ['09:00 AM' => '12:00 AM'],
'Fri' => ['09:00 AM' => '12:00 AM'],
'Sat' => ['12:00 AM' => '01:00 AM']
];
困难的部分是周六,阵列的末尾,周日中午12:00重新开放。
任何人都可以提供帮助,或者请指出正确的方向。
答案 0 :(得分:2)
您使用日期(' D')来获取缩写的工作日。
并使用array_keys()/ array_values()来获取内部数组的hte键/值。
<?php
$today_opening = array_keys($storeSchedule[date('D')]);
$today_opening = $today_opening [0];
$today_closing = array_values($storeSchedule[date('D')]);
$today_closing = $today_closing [0];
$tomorrow_opening = array_keys($storeSchedule[date('D', time()+24*60*60)]);
$tomorrow_opening = $tomorrow_opening [0];
if (strtotime("today " . $today_opening) > time())
echo "Opens at " . $today_opening;
elseif (strtotime("today " . $today_closing) > time())
echo "Still open until " . $today_closing;
else
echo "Opens tomorrow at " . $tomorrow_opening;
答案 1 :(得分:1)
PHP有date('w')
,它给出了今天的周数,周日为0,星期六为6。您可以像这样修改代码:
$storeSchedule = [
0 => ['12:00 AM' => '01:00 AM'],
1 => ['09:00 AM' => '12:00 AM'],
2 => ['09:00 AM' => '12:00 AM'],
3 => ['09:00 AM' => '12:00 AM'],
4 => ['09:00 AM' => '12:00 AM'],
5 => ['09:00 AM' => '12:00 AM'],
6 => ['12:00 AM' => '01:00 AM']
];
// tomorrow's week number
$tomorrow = date('w') + 1;
if ($tomorrow > 6) { // On Saturdays, the above statement will return 7
$tomorrow = 0; // Set manually to Sunday's code
}
$openingHours = $storeSchedule[$tomorrow];
print_r($openingHours);
答案 2 :(得分:1)
不确定这是否足够,但商店接下来会在第二天营业,所以: -
$storeSchedule = array(
'Sun' => array('12:00 AM','01:00 AM'),
'Mon' => array('09:00 AM','12:00 AM'),
'Tue' => array('09:00 AM','12:00 AM'),
'Wed' => array('09:00 AM','12:00 AM'),
'Thu' => array('09:00 AM','12:00 AM'),
'Fri' => array('09:00 AM','12:00 AM'),
'Sat' => array('12:00 AM','01:00 AM')
);
$next=date('D', strtotime('+1 day') );
$times=$storeSchedule[ $next ];
$openingtime=$times[0];
$closingtime=$times[1];
echo 'Next opens: ' . $next . ' @' . $openingtime.' and closes @'.$closingtime;
我稍微重新调整了数组,因为我使用的是php版本,所以我不能使用新的[]语法 - 因此是老式的。
或者,使用key-&gt;值对作为原始
$storeSchedule = array(
'Sun' => array('12:00 AM'=>'01:00 AM'),
'Mon' => array('09:00 AM'=>'12:00 AM'),
'Tue' => array('09:00 AM'=>'12:00 AM'),
'Wed' => array('09:00 AM'=>'12:00 AM'),
'Thu' => array('09:00 AM'=>'12:00 AM'),
'Fri' => array('09:00 AM'=>'12:00 AM'),
'Sat' => array('12:20 AM'=>'01:00 AM')
);
$next=date('D', strtotime('+1 day') );
$times=$storeSchedule[ $next ];
$keys=array_keys( $times );
$values=array_values( $times );
$openingtime=$keys[0];
$closingtime=$values[0];
echo 'Next opens: ' . $next . ' @' . $openingtime.' and closes @'.$closingtime;