[RESOLVED] Grouping business hours that are the same?
Results 1 to 3 of 3

Thread: [RESOLVED] Grouping business hours that are the same?

  1. #1

    resolved [RESOLVED] Grouping business hours that are the same?

    I'm putting together some location pages. I'm trying to figure out the best way to group the hours if they are exactly the same.

    Results before code below.

    Code:
    Mon		8:30 a.m. - 4:30 p.m.
    Tues		8:30 a.m. - 4:30 p.m.
    Wed		8:30 a.m. - 4:30 p.m.
    Thur		8:30 a.m. - 4:30 p.m.
    Fri		8:30 a.m. - 5:00 p.m.
    Sat		8:00 a.m. - 12:00 p.m.
    Sun		Closed

    Results after implementing code below.

    Code:
    Mon		8:30 a.m. - 4:30 p.m.
    Fri		8:30 a.m. - 5:00 p.m.
    Sat		8:00 a.m. - 12:00 p.m.
    Sun		Closed
    Result I'm trying to achieve. Basically I need to append the last day of a grouped series. So, append Thursday after Monday. I'm hoping that this will also be dynamic. So if the groupings change it's smart enough to append the last day as needed.

    Code:
    Mon-Thur	8:30 a.m. - 4:30 p.m.
    Fri		8:30 a.m. - 5:00 p.m.
    Sat		8:00 a.m. - 12:00 p.m.
    Sun		Closed
    This code does everything minus append the last day of a grouped series. After staring at for a few hours, I'm not sure this will work? I tried adding a second foreach loop to build the days that are causing the if statement below to trigger. Then using that new array to print the data. No luck though. Anyone wanna point me in a direction?

    PHP Code:
    <?php
     
    // Set the variables for open and close
     
    $open '';
     
    $close '';
     foreach (
    $lobby as $key => $hours) {
         
         
    // Check the open and close variables and if the same group by days of the week
         
    if (($open != $hours['hours_open'] || $close != $hours['hours_close']) && $hours['hours_notopen'] != 'yes') {
             echo 
    '<dd>'.$days[$hours['hours_dayofweek']].'</dd>';
             echo 
    '<dd class="hours">'.ltrim($hours['hours_open'], '0'). ' a.m. - '.ltrim($hours['hours_close'], '0'). ' p.m.</dd>';
             
             
    $open $hours['hours_open'];
             
    $close $hours['hours_close'];
         }

         if (
    $hours['hours_notopen'] == 'yes') {
             echo 
    '<dd>'.$days[$hours['hours_dayofweek']].'</dd>';
             echo 
    '<dd>Closed</dd>';
         }
         
    ?>
     <?
     
    }
     
    ?>

  2. #2
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,932
    You sort of have two issues at stake here. One is grouping the week days according to their hour ranges. That in itself is not particularly hard:
    PHP Code:
    $grouped_by_hours = array();
    foreach (
    $lobby as $key => $hours) {
      
    // Check the open and close variables and if the same group by days of the week
      
    if (($open != $hours['hours_open'] || $close != $hours['hours_close']) && $hours['hours_notopen'] != 'yes') {
        
    // this string is the hours open range 
        
    $hour_string ltrim($hours['hours_open'], '0'). ' a.m. - '.ltrim($hours['hours_close'], '0');
        if (!
    array_key_exists($hour_string$grouped_by_hours)) {
          
    // first item with this hour range, start an array
          
    $grouped_by_hours[$hour_string] = array($days[$hours['hours_dayofweek']]);
        } else {
          
    // not the first item with this hour range, append to the array
          
    $grouped_by_hours[$hour_string][] = array($days[$hours['hours_dayofweek']]);
        }
      }
    }

    // once grouped by hours open, output them
    foreach($grouped_by_hours as $hours_open => $days_open_for_this_range) {
      echo 
    implode(","$days_open_for_this_range) . " - " $hours_open "<br>\n";

    The second issue is converting contiguous days (e.g., Mon, Tue, Wed) into a range.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  3. #3
    Hey Sneaky, thanks for the reply. Unfortunately I couldn't get your version to give me the results I was after. That's not to say it couldn't be tweaked here and there and it would work. I did find another method very similar to yours that did work. I'm sure I could apply some of the method below to yours and end up with the same result.

    Here is what did work for anyone with a similar issue. Hopefully it makes sense or is somewhat useful.

    PHP Code:
    /*** Build the banking center and atm lists ***/
    function buildHours($hours_type) {
        
    // I needed this array because I store the days of the week as intergers in the database
        
    $days = array(=> 'Mon'=> 'Tues'=> 'Wed'=> 'Thur'=> 'Fri'=> 'Sat'=> 'Sun');
        
        foreach (
    $hours_type as $hours) {
            if (
    $hours['hours_notopen'] != 'yes') {
                
    $openHours[] = $hours['hours_open'].' a.m. - '.$hours['hours_close'].' p.m.';
            }
            else {
                
    $openHours[] = 'Closed';
            }
        }
        
    //combne the days array with the data array above
        
    $openHours array_combine($days$openHours);
        
        
    // create the array that will match days to hours open for each day
        // this will group them by matching hours
        
    $summary = array();
        foreach (
    $openHours as $day => $hours) {
            if (!
    array_key_exists($hours$summary)) {
                
    $summary[$hours] = array($day);
            }
            else {
                
    $summary[$hours][] = $day;
            }
        }
        
        
    $theHours '';
        foreach (
    $summary as $hours => $days) {
            if (
    count($days) > 1) {
                
    // If the hours match span multiple days assign this
                
    $theHours .= '<dd>'.array_shift($days) . ' - ' array_pop($days) . '</dd><dd>' $hours .'</dd>' "\n";
            }
            else {
                
    // If the hours are specific to only one day assign this
                
    $theHours .= '<dd>'.array_shift($days) . '</dd><dd>' $hours .'</dd>' "\n";
            }
        }
        return 
    $theHours;


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •