Skip to content
Advertisement

How to get weekday with in out time if same time day in days to we will show like Monday-Tuesday 10:00-18:00

How to get weekday with in out time if same time day in days to we will show like Monday-Tuesday 10:00-18:00 Array ( [0] => Array ( [day] => Sunday [in_time] => [out_time] => [active_day] => 0 )

[1] => Array
    (
        [day] => Monday
        [in_time] => 10:00
        [out_time] => 18:00
        [active_day] => 1
    )

[2] => Array
    (
        [day] => Tuesday
        [in_time] => 10:00
        [out_time] => 18:00
        [active_day] => 1
    )

[3] => Array
    (
        [day] => Wednesday
        [in_time] => 12:00
        [out_time] => 16:00
        [active_day] => 1
    )

[4] => Array
    (
        [day] => Thursday
        [in_time] => 12:00
        [out_time] => 16:00
        [active_day] => 1
    )

[5] => Array
    (
        [day] => Friday
        [in_time] => 10:00
        [out_time] => 19:00
        [active_day] => 1
    )

[6] => Array
    (
        [day] => Saturday
        [in_time] => 
        [out_time] => 
        [active_day] => 0
    )

)

Advertisement

Answer

Simply create an associative array indexed by the time frames – and then aggregate the days by their time frame:

<?php
$result = [];

foreach($list as $item)
{
  if ($item['active_day']) $result[$item['in_time'].'-'.$item['out_time'][] = $item['day'];
}

foreach($result as $time => $days)
{
  echo $days[0];
  $numDays = count($days);
  if ($numDays > 1) echo '-'.$days[$numDays - 1];
  echo ' '.$time."n";
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement