function UTIsOpen() {
$status = FALSE;
//get current East Coast US time
$timeObject = new DateTime('America/Chicago');
$timestamp = $timeObject->getTimeStamp();
$currentTime = $timeObject->setTimestamp($timestamp)->format('H:i A');
$today = date("M-d-Y");
//January 2nd – January 20th
$janA = date("M-d-Y", mktime(0,0,0,1,1,2018));
$janB = date("M-d-Y", mktime(0,0,0,1,21,2018));
//January 22nd – February 10th
$janC = date("M-d-Y", mktime(0,0,0,1,21,2018));
$febB = date("M-d-Y", mktime(0,0,0,2,11,2018));
//February 12th – March 3rd
$febC = date("M-d-Y", mktime(0,0,0,2,11,2018));
$marA = date("M-d-Y", mktime(0,0,0,3,4,2018));
//March 5th – April 16th
$marB = date("M-d-Y", mktime(0,0,0,3,4,2018));
$aprA = date("M-d-Y", mktime(0,0,0,4,17,2018));
//April 17th
$aprB = date("M-d-Y", mktime(0,0,0,4,17,2018));
if (($today > $janA) && ($today < $janB)) {
$storeSchedule = array( //January 2nd – January 20th
'Mon' =>
array('08:00 AM' => '06:00 PM'),
'Tue' =>
array('08:00 AM' => '06:00 PM'),
'Wed' =>
array('08:00 AM' => '06:00 PM'),
'Thu' =>
array('08:00 AM' => '06:00 PM'),
'Fri' =>
array('08:00 AM' => '06:00 PM'),
'Sat' =>
array('09:00 AM' => '03:00 PM')
);
}
elseif (($today > $janC) && ($today < $febB)) {
$storeSchedule = array( //January 22nd – February 10th
'Mon' =>
array('07:00 AM' => '10:00 PM'),
'Tue' =>
array('07:00 AM' => '10:00 PM'),
'Wed' =>
array('07:00 AM' => '10:00 PM'),
'Thu' =>
array('07:00 AM' => '10:00 PM'),
'Fri' =>
array('07:00 AM' => '10:00 PM'),
'Sat' =>
array('08:00 AM' => '06:00 PM')
);
}
elseif (($today > $febC) && ($today < $marA)) {
$storeSchedule = array( //February 12th – March 3rd
'Mon' =>
array('08:00 AM' => '08:00 PM'),
'Tue' =>
array('08:00 AM' => '08:00 PM'),
'Wed' =>
array('08:00 AM' => '08:00 PM'),
'Thu' =>
array('08:00 AM' => '08:00 PM'),
'Fri' =>
array('08:00 AM' => '08:00 PM'),
'Sat' =>
array('09:00 AM' => '04:00 PM')
);
}
elseif (($today > $marB) && ($today < $aprA)) {
$storeSchedule = array( //March 5th – April 16th
'Mon' =>
array('08:00 AM' => '06:00 PM'),
'Tue' =>
array('08:00 AM' => '06:00 PM'),
'Wed' =>
array('08:00 AM' => '06:00 PM'),
'Thu' =>
array('08:00 AM' => '06:00 PM'),
'Fri' =>
array('08:00 AM' => '06:00 PM'),
'Sat' =>
array('09:00 AM' => '02:00 PM')
);
}
elseif ($today == $aprB) {
$storeSchedule = array( //April 17th
'Mon' =>
array('08:00 AM' => '05:00 PM'),
'Tue' =>
array('08:00 AM' => '08:00 PM'),
'Wed' =>
array('08:00 AM' => '05:00 PM'),
'Thu' =>
array('08:00 AM' => '05:00 PM'),
'Fri' =>
array('08:00 AM' => '05:00 PM')
);
}
else {
$storeSchedule = array(
'Mon' =>
array('08:00 AM' => '05:00 PM'),
'Tue' =>
array('08:00 AM' => '05:00 PM'),
'Wed' =>
array('08:00 AM' => '05:00 PM'),
'Thu' =>
array('08:00 AM' => '05:00 PM'),
'Fri' =>
array('08:00 AM' => '05:00 PM')
);
};
// loop through time ranges for current day
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {
// create time objects from start/end times and format as string (24hr AM/PM)
$startTime = DateTime::createFromFormat('h:i A', $startTime)->format('H:i A');
$endTime = DateTime::createFromFormat('h:i A', $endTime)->format('H:i A');
// check if current time is within the range
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$status = TRUE;
echo '<a href="tel:383-327-1910" id="number_link"><span itemprop="telephone">383-327-1910</span></a>';
} else {
echo "Phone support is only available during business hours.";
}
}
}
我是PHP的新手,我在上面的代码中尝试实现的是使用基于当前日期的数组。我遇到麻烦的是当我将日期设置为2018年1月21日(测试)时,它使用了错误的数组。善于解释的人能否帮助我对编码有所了解?我知道这个多了,如果已经坏了,应该转换为开关 - 我还不知道怎么做 - 谢谢。
答案 0 :(得分:0)
您的代码在1月21日失败,因为即使$janB
和$janC
设置为该日期,您的两个条件语句都不包括在1月21日。
if (($today > $janA) && ($today < $janB)) {
// This one is only true if $today is before Jan 21st
}
elseif (($today > $janC) && ($today < $febB)) {
// This one is only true if $today is after Jan 21st
}
您在代码中进行了大量不必要的DateTime转换。
由于您每周都在改变工作时间,因此您只需检查周数,日期编号和小时编号,而不是创建大量日期,如下所示:
$day = date("N"); // Day as a number, 1 = Monday, 7 = Sunday
$hour = date("G"); // Hour as a number, 24 hour clock
$week = date("W"); // Week of the year as a number, 1 - 52
switch ($week) { // Check what week number it is
case 01:
case 02:
case 03: // Schedule for first three weeks
$storeSchedule = array('Week' => array('open'=> 8, 'close' => 18),'Sat' => array('open'=> 9, 'close' => 15));
break; // Hours are the same Mon - Fri, so just save 1 value for week days and one for Sat. Use hour from 24 hour clock
case 04:
case 05:
case 06:
$storeSchedule = // Hours here
break;
case 07:
case 08:
case 09:
$storeSchedule = // Hours here
break;
}
if (date('M-d-Y') == date("M-d-Y", strtotime("17 apr 2018"))) {
$storeSchedule = array('Week' => array('open'=> 8, 'close' => 20));
}
if ($day < 6) {
$hours = $storeSchedule['Week']; // Set hours for weekday
}
if ($day == 6) { // Unless its Sat
$store = $storeSchedule['Sat'];
}
if ($hour >= $store['open'] && $hour < $store['close'] && $day != 7) { // Check its not Sunday and between open and close hours
echo '<a href="tel:383-327-1910" id="number_link"><span itemprop="telephone">383-327-1910</span></a>';
}
else {
echo "Phone support is only available during business hours.";
}