PHP获得工作日时间
我有一个代码,可以在MySQL中为特定业务节省工作时间和工作日。从MySQL获取数据时,输出如下:PHP获得工作日时间,php,mysql,datetime,Php,Mysql,Datetime,我有一个代码,可以在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 ) 0100简单的意思是周一上午10点到12点,如果是这样的话01518意思是周一下午3点到6点。这不是问题所在,因为我已经通过以下代码解决了这个问题: $openHrs = explode(",", $openHrs['open_hours']
Array ( [open_hours] => 0 10 0,1 10 0,2 10 0,3 10 0,4 10 0,5 10 0,6 10 0 )
0100
简单的意思是周一上午10点到12点
,如果是这样的话01518
意思是周一下午3点到6点
。这不是问题所在,因为我已经通过以下代码解决了这个问题:
$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];
}
现在,我如何编写更高级的代码,以便如果今天是星期一,我只需要显示星期一的输出结果?哪一个是
0100
还是Mon 10am 12am
?感谢您的帮助如果您想过滤掉今天工作日的开放时间
,下面的代码就可以了
$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];
}
}
你可以用这个代码解决这个问题
for($i=0;$i<=6;$i++)
{
if($res[$i]==date('w')) //get today's week number
{
echo $res[$i]." ".$temp[1]." ".$temp[2];
}
}
对于($i=0;$i这听起来是个棘手的问题,但看看是否值得尝试一下:
$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';
}
这只是我能想到的概念,当然代码可能需要一些操作!在上面的代码中,你在哪里存储一天的数值???@Sriniwas你是什么意思?没什么..只是一个误解...)
$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];
$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';
}