I have a code where I can save business hours and weekday for specific business in MySQL. When the data is fetched from MySQL the output will be as follow:
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
simply means Mon 10am 12am
and if it was like this 0 15 18
means Mon 3pm 6pm
. This is not the issue as I already solved this by the following code :
$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];
}
Now how can I code more advanced so that if today was Monday I need only to display the output result of Monday ? Which is 0 10 0
or Mon 10am 12am
? Thanks for the help
U can solve this problem with this code
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];
}
}
If you want to filter out the open_hours
with the weekday for today, the below code should do.
$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];
}
}
$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];
Well it sounds like a tough question but see if it is worth trying this:
$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';
}
This is only the concept that I could think of, surely there could be some manipulations needed for the code!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.