Strahan Posted August 12, 2014 Share Posted August 12, 2014 (edited) I have a weird issue. I'm working on something where people will submit hours for a business. Originally it was free form text, but then I figured it may get messy with people doing M-F 9-5 then Monday through Friday, 9AM to 5PM, Mon-Fri, etc. So I switched to a bunch of inputs. It has each day then a select for from and one for to. Data is passed to the script as a form submission. Vars are named HF# and HT# (hours from, hours to and a 0-6 digit for Mon-Sun). I want to take the input and output a string as such: Mon-Tue, 8:00 AM to 5:00 PM Thu-Fri, 8:00 AM to 5:00 PM Sat, 9:00 AM to 1:00 PM Sun, 12:00 PM to 3:00 PM I did this: $formdata = array(); $formdata["hf0"] = "8:00 AM"; $formdata["ht0"] = "4:30 PM"; $formdata["hf1"] = "8:00 AM"; $formdata["ht1"] = "4:30 PM"; $formdata["hf2"] = ""; $formdata["ht2"] = ""; $formdata["hf3"] = "8:00 AM"; $formdata["ht3"] = "4:30 PM"; $formdata["hf4"] = "8:00 AM"; $formdata["ht4"] = "4:30 PM"; $formdata["hf5"] = "9:00 AM"; $formdata["ht5"] = "1:00 PM"; $formdata["hf6"] = "12:00 PM"; $formdata["ht6"] = "3:00 PM"; $WeekDays = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); $hours = ""; $sd = "Mon"; $ld = ""; $fr = $formdata["hf0"]; $to = $formdata["ht0"]; for ($x=0; $x<=6; $x++) { if (empty($sd) && !empty($formdata["hf$x"])) { $sd = $WeekDays[$x]; $fr = $formdata["hf$x"]; $to = $formdata["ht$x"]; } if ($fr != $formdata["hf$x"] || $to != $formdata["ht$x"]) { $hours .= "$sd" . (($sd==$ld)?", ":" - $ld, ") . " $fr to $to<br/>"; if (!empty($formdata["hf$x"])) { $sd = $WeekDays[$x]; $fr = $formdata["hf$x"]; $to = $formdata["ht$x"]; } else { $sd = ""; } } $ld = $WeekDays[$x]; } ($formdata was because I was tired of hitting back, changing the data then resubmitting) This kinda works, but it skips Sunday. There has got to be a better way to do this.. any ideas? Edited August 12, 2014 by Strahan Quote Link to comment https://forums.phpfreaks.com/topic/290431-formatting-daily-hours/ Share on other sites More sharing options...
Strahan Posted August 13, 2014 Author Share Posted August 13, 2014 (edited) OK, I got it working. However, I'm still curious if there is a better way to do this as my solution seems awful awkward: $fd = array(); $fd["hf0"] = "8:00 AM"; $fd["ht0"] = "4:30 PM"; $fd["hf1"] = "8:00 AM"; $fd["ht1"] = "4:30 PM"; $fd["hf2"] = ""; $fd["ht2"] = ""; $fd["hf3"] = "9:00 AM"; $fd["ht3"] = "1:00 PM"; $fd["hf4"] = ""; $fd["ht4"] = ""; $fd["hf5"] = "9:00 AM"; $fd["ht5"] = "1:00 PM"; $fd["hf6"] = ""; $fd["ht6"] = ""; $WeekDays = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); $hours = ""; $sd = ""; $ld = ""; $sxnt = true; $fr = ""; $to = ""; for ($x=0; $x<=5; $x++) { if (empty($fd["hf$x"])) { $hours .= "$sd" . ((empty($ld))?", ":" - $ld") . " $fr to $to<br/>{$WeekDays[$x]}, Closed<BR>"; $sd = ""; $ld = ""; } else { if (empty($sd)) { $sd = $WeekDays[$x]; $fr = $fd["hf$x"]; $to = $fd["ht$x"]; } if (empty($ld) && $sd != $WeekDays[$x]) $ld = $WeekDays[$x]; if ($fd["hf$x"] != $fr || $fd["ht$x"] != $to) { $hours .= "$sd - $ld, $fr to $to<BR>"; $sd = $WeekDays[$x]; $fr = $fd["hf$x"]; $to = $fd["ht$x"]; $ld = ""; } else { if ($sd != $WeekDays[$x]) $ld = $WeekDays[$x]; } } } if ($fd["hf6"] != $fr || $fd["ht6"] != $to) { $hours .= "$sd" . ((empty($ld))?", ":" - $ld") . " $fr to $to<br/>Sun, " . ((empty($fd["hf6"]))?"Closed":"{$fd["hf6"]} to {$fd["ht6"]}") . "<BR>"; } else { $hours .= "$sd - Sun, $fr to $to<BR>"; } Which gives me: Mon - Tue 8:00 AM to 4:30 PM Wed, Closed Thu, 9:00 AM to 1:00 PM Fri, Closed Sat, 9:00 AM to 1:00 PM Sun, Closed Edited August 13, 2014 by Strahan Quote Link to comment https://forums.phpfreaks.com/topic/290431-formatting-daily-hours/#findComment-1487637 Share on other sites More sharing options...
Solution mac_gyver Posted August 13, 2014 Solution Share Posted August 13, 2014 this is actually about the same logic as in your last post, but eliminates the extra code at the end to finish the last day of the week - <?php $fd = array(); $fd[0] = array('f'=>"8:00 AM",'t'=>"4:30 PM"); $fd[1] = array('f'=>"8:00 AM",'t'=>"4:30 PM"); $fd[2] = array('f'=>"",'t'=>""); $fd[3] = array('f'=>"9:00 AM",'t'=>"1:00 PM"); $fd[4] = array('f'=>"",'t'=>""); $fd[5] = array('f'=>"9:00 AM",'t'=>"1:00 PM"); $fd[6] = array('f'=>"",'t'=>""); $WeekDays = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",''); // the extra value on the end processes the last day without repeating any logic $last_from = null; $last_to = null; $output = ''; $last = count($WeekDays)-1; foreach($WeekDays as $key=>$day){ if($key == $last || $last_from !== $fd[$key]['f'] || $last_to !== $fd[$key]['t']){ // it's past the end or the from/to is not-equal to last values if($last_from !== null){ // not the first section, build the section that just ended $end_key = $key-1; $op = ($fd[$end_key]['f'] != '') ? "{$fd[$end_key]['f']} to {$fd[$end_key]['t']}" : 'Closed'; $dy = ($start_day == $WeekDays[$end_key]) ? $start_day : "$start_day - {$WeekDays[$end_key]}"; $output .= "$dy $op<br>"; } // start a new section $last_from = isset($fd[$key]) ? $fd[$key]['f'] : ''; $last_to = isset($fd[$key]) ? $fd[$key]['t'] : ''; // remember the new values $start_day = $day; } } echo $output; the data array is organized differently to make the references clearer. you can change the form field names to submit the data as shown or change the references in the code to match your current form field names. Quote Link to comment https://forums.phpfreaks.com/topic/290431-formatting-daily-hours/#findComment-1487653 Share on other sites More sharing options...
Strahan Posted August 15, 2014 Author Share Posted August 15, 2014 Thanks a lot, I'll implement your code in place of mine. I appreciate the help Quote Link to comment https://forums.phpfreaks.com/topic/290431-formatting-daily-hours/#findComment-1487913 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.