我有一个应用程序,我可以在 MySql 上保存工作日和营业时间,如下所示:
0 10 22,1 10 22,2 10 22,4 10 22,5 10 22,6 10 22
php 数组从 Mysql 获取它,格式如下
Array ( [open_hours] => 0 10 22,1 10 22,2 10 22,4 10 22,5 10 22,6 10 22 )
0 10 22
只是意味着Monday 10am 22pm
我当前的代码似乎效果不佳,下面是我用来格式化日期和时间的代码
$openHrs = $businessMapper->getBusinessHours($business_id);
// return Array ( [open_hours] => 0 10 22,1 10 22,2 10 22,4 10 22,5 10 22,6 10 22 )
$openHrs = explode(",", $openHrs['open_hours']);
$weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
foreach($openHrs as &$temp) {
//$temp = $weekdays[$temp[0]]
$temp = explode(" ", $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';
$temp = $weekdays[$temp[0]] . ' ' . $temp[1] . ' ' . $temp[2];
}
但问题是,我只得到一个结果,即Sat 10am 10pm
. 我怎样才能修复我的代码?谢谢!!