我有一个代码,可以节省MySQL中特定业务的营业时间和工作日。从MySQL获取数据时,输出如下:
Array ( [open_hours] => 0 10 0,1 10 0,2 10 0,3 10 0,4 10 0,5 10 0,6 10 0 )
0 10 0
只是意味着Mon 10am 12am
,如果它是0 15 18
意味着Mon 3pm 6pm
。这不是问题,因为我已经通过以下代码解决了这个问题:
$openHrs = explode(",", $openHrs['open_hours']);
$weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$res = array();
foreach($openHrs as &$temp) {
$temp = explode(" ", $temp);
$temp[1] = $temp[1] > 12 ? $temp[1] - 12 . 'pm' : $temp[1] . 'am';
$temp[2] = $temp[2] > 12 ? $temp[2] - 12 . 'pm' : $temp[2] . 'am';
$res[] = $weekdays[$temp[0]] . ' ' . $temp[1] . ' - ' . $temp[2];
}
现在我如何编码更高级的代码,以便如果今天是星期一,我只需要显示星期一的输出结果?哪个是0 10 0
或Mon 10am 12am
?谢谢你的帮助
答案 0 :(得分:2)
U可以使用此代码解决此问题
for($i=0;$i<=6;$i++)
{
if($res[$i]==date('w')) //get today's week number
{
echo $res[$i]." ".$temp[1]." ".$temp[2];
}
}
OR
for($i=0;$i<=6;$i++)
{
if($res[$i]==date('D')) // get today's weekday
{
echo $res[$i]." ".$temp[1]." ".$temp[2];
}
}
答案 1 :(得分:1)
如果您想在今天的工作日过滤掉open_hours
,则可以使用以下代码。
$openHrs = explode(",", $openHrs['open_hours']);
$weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$res = array();
$todayWeekDay = date('D'); // get today's weekday
$todayWeekNum = array_search($todayWeekDay, $weekdays); // get number for today's weekday
foreach($openHrs as &$temp) {
$temp = explode(" ", $temp);
$temp[1] = $temp[1] > 12 ? $temp[1] - 12 . 'pm' : $temp[1] . 'am';
$temp[2] = $temp[2] > 12 ? $temp[2] - 12 . 'pm' : $temp[2] . 'am';
// only add the item where the weekday is equal to today's
if ($temp[0] == $todayWeekNum) {
$res[] = $weekdays[$temp[0]] . ' ' . $temp[1] . ' - ' . $temp[2];
}
}
答案 2 :(得分:0)
$i; // the weekday; comes as input
$openHrs = explode(",", $openHrs['open_hours']);
$weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$res = array();
$temp = explode(" ", $openHrs[$i]);
$temp[1] = $temp[1] > 12 ? $temp[1] - 12 . 'pm' : $temp[1] . 'am';
$temp[2] = $temp[2] > 12 ? $temp[2] - 12 . 'pm' : $temp[2] . 'am';
$res = $weekdays[$temp[0]] . ' ' . $temp[1] . ' - ' . $temp[2];
答案 3 :(得分:0)
这听起来像是一个棘手的问题,但看看是否值得尝试:
$open_hours = array(Mon 10 0,Tue 10 0,Wed 10 0,Thu 10 0,Fri 10 0,Sat 10 0,Sun 10 0 );
$today = date('D');
$associate_array_key = array_keys($open_hours, $today);
$openHrs = explode(" ", $open_hours[$associate_array_key]);
foreach($openHrs as &$temp) {
$temp[0];
$temp[1] = $temp[1] > 12 ? $temp[1] - 12 . 'pm' : $temp[1] . 'am';
$temp[2] = $temp[2] > 12 ? $temp[2] - 12 . 'pm' : $temp[2] . 'am';
}
这只是我能想到的概念,当然可能需要对代码进行一些操作!