1

i have an app where i save weekdays and business hours on MySql as follow:

0 10 22,1 10 22,2 10 22,4 10 22,5 10 22,6 10 22

The php array fetch this from Mysql as following formats

Array ( [open_hours] => 0 10 22,1 10 22,2 10 22,4 10 22,5 10 22,6 10 22 )

0 10 22 simply means Monday 10am 22pm

My current code seems not works well, below are the code i use to format the date and time

$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];
        }

But the issues is, i only get one result which is Sat 10am 10pm. How can i fix my code? Thanks!!

1
  • What output do you get? Commented Feb 26, 2013 at 8:47

3 Answers 3

1

Issue : every time when foreach iterates it over writes the previous value ie $temp will contains only the last value.

Solution : Added a variable $res as array and assigned each value to it.

Try this :

$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');
$res      = array();
        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';
            $res[]   = $weekdays[$temp[0]] . ' ' . $temp[1] . ' ' . $temp[2];
        }

echo "<pre>";
print_r($res);
Sign up to request clarification or add additional context in comments.

Comments

0

foreach($openHrs as &$temp)

you cannot use $temp variable in your loop!

Comments

0
$openHrs = $businessMapper->getBusinessHours($business_id);
$openHrs = explode(",", $openHrs['open_hours']);
$weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');

$resultArray = array();

foreach($openHrs as $temp) {
   $tempRecord = explode(" ", $temp);

   if (count($tempRecord) == 3) {
       $timeBegin = $tempRecord[1] > 12 ? $tempRecord[1] - 12. 'pm' : $tempRecord[1]. 'am';
       $timeEnd = $tempRecord [2] > 12 ? $tempRecord[2] - 12. 'pm' : $tempRecord[2]. 'am';
       $resultArray[] = "{$weekdays[$temp[0]]} {$timeBegin} {$timeEnd}";
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.