Generate days of week from comma separated values in PHP

549 views Asked by At

I have an array in PHP where I store day values

$days = array (
    1 => 'Mon', 
    2 => 'Tue',
    3 => 'Wed',
    4 => 'Thu',
    5 => 'Fri',
    6 => 'Sat',
    7 => 'Sun'
);

I have a comma separated values which denote the keys of the variable $days. If the series is

1,2,3,4,5,6,7 then it should dispaly as "Mon - Sun".

1,2,3,4,5 then it should dispaly as "Mon - Fri".

1,2,4,5,6 then it should display "Mon - Tue, Thu - Sat"

1,3,5,7 then it should display "Mon, Wed, Fri, Sun"

The below code will generate only "Mon - Tue - Wed - Thu - Fri" for the series 1,2,3,4,5

    $keys  = "1,2,3,4,5,6";
    $days = array('1' => 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');

    $key_array = explode(',', $keys);

    $day_series = $first_day = $days[reset($key_array)];
    $first_value = reset($key_array);

    foreach ($key_array as $key => $value) {
        if ($first_value != $value) {
            if ($value == $i) {
               $day_series = $day_series . ' - ' . $days[$value];
            } else
                $day_series = $day_series  . ', ' . $days[$value];
        }
        $i++;
    }
    echo $day_series;
2

There are 2 answers

1
bigbobr On

Remove else from your foreach loop like this

foreach ($key_array as $key => $value) {
        if ($first_value != $value) {
            if ($value == $i) {
               $day_series = $day_series . ' - ' . $days[$value];
            }         
        $i++;
        }
    }
    $day_series = $day_series  . ', ' . $days[$value];
    echo $day_series;

This should work to give you Mon-Fri for 1,2,3,4,5

0
Scott R On

It's clunky but it works.

$DAY_NUMBER = array( 'Mon' => 0, 'Tue' => 1, 'Wed' => 2, 'Thu' => 3, 'Fri' => 4, 'Sat' => 5, 'Sun' => 6 );

$NUMBER_DAY = array( 0 => 'Mon', 1 => 'Tue', 2 => 'Wed', 3 => 'Thu', 4 => 'Fri', 5 => 'Sat', 6 => 'Sun' );


function day_series ( $day_list ) {

        global $DAY_NUMBER, $NUMBER_DAY;

        // CONVERT DAY NAMES TO NUMBERS
        $num_list = array();
        for( $n = 0; $n < count($day_list); $n++ ) {
                $num_list[] = $DAY_NUMBER[$day_list[$n]];
        }

        // ALLOW FOR WRAP AROUND
        $first = $num_list[0];
        for( $n = 0; $n < count($num_list); $n++ ) {
                if( $num_list[$n] < $first ) {
                        $num_list[$n] += 7;
                }
        }

        $day_series = array();

        while( !empty($num_list) ) {

                $first = $last = array_shift($num_list);

                while( !empty($num_list) && ( $last+1 == $num_list[0] ) ) {
                        $last = array_shift($num_list);
                }

                if( $first == $last ) {
                        $day_series[] = $NUMBER_DAY[ $first % 7 ];
                } else if( $first+1 == $last ) {
                        $day_series[] = $NUMBER_DAY[ $first % 7 ] . ',' . $NUMBER_DAY[ $last % 7 ];
                } else {
                        $day_series[] = $NUMBER_DAY[ $first % 7 ] . '-' . $NUMBER_DAY[ $last % 7 ];
                }
        }

        // RETURN AS JOINED STRING
        return join(',', $day_series );
}