I’m trying to define the logic that would return a value of the next live show time per day for a media station.
The array
$liveshows = [ 'mon' => [ ['start' => '0600', 'end' => '0900', 'host' => 'Joe'], ['start' => '1300', 'end' => '1500', 'host' => 'Carol'], ['start' => '1500', 'end' => '1600', 'host' => 'Cortez'], ['start' => '1700', 'end' => '2100', 'host' => 'Boy George'] ], ];
Process
date_default_timezone_set('America/New_York'); $day = strtolower(date('D')); $current_time = date('Hi'); $html=''; $start=[]; if( $day == 'mon' && !empty( $liveshows['mon'] ) ) { foreach( $liveshows['mon'] as $showtime ) { if( $current_time >= $showtime['start'] && $current_time <= $showtime['end'] ) $html .= '<h3>' . $showtime['host'] . ' <span>is on air</span></h3>'; // create an array of start times to use in below 'no live show' notice. $start[] = $showtime['start']; } } // print it echo $html;
All works fine to that point.
Now I want to show a notice when there is no live host on air so I’ve used current()
, next()
and end()
to cycle the $start
array. It works to a point but fails because the array count()
is not consistent per day.
if( empty( $html ) ) { $nextshow=''; if( $current_time < current($start) ) { $nextshow = current($start); } elseif( $current_time < next($start) ) { $nextshow = next($start); } echo '<h3>No live show is on</h3>'; echo '<p>The next live show is at ' . date('g:iA', strtotime( $nextshow )) . '</p>'; }
The output notice when retrieved is:
From midnight
The next live show is at 6:00AM
Between first show and next
The next live show is at 1:00PM
After the 1600 (4PM) show end time, the process fails and the output is without the 5PM value for the next in array and just…
The next live show is
How do I correctly scan the array and compare to current time to check if a show is on, and if not, show the time of the next show in line?
Advertisement
Answer
I think it’s quite cumbersome to use current/next
in your case. You can simplify your code greatly by keeping track on whether any show is on air $isOnAir = true/false
(in below code) and if not, simply reiterating the shows array with a different condition.
Example:
<?php declare(strict_types=1); date_default_timezone_set('America/New_York'); $day = strtolower(date('D')); $current_time = date('Hi'); // test $day = 'mon'; $current_time = '1630'; $liveshows = [ 'mon' => [ ['start' => '0600', 'end' => '0900', 'host' => 'Joe'], ['start' => '1300', 'end' => '1500', 'host' => 'Carol'], ['start' => '1500', 'end' => '1600', 'host' => 'Cortez'], ['start' => '1700', 'end' => '2100', 'host' => 'Boy George'], ], ]; if ($day === 'mon' && !empty($liveshows['mon'])) { // assume no show is on air $isOnAir = false; foreach ($liveshows['mon'] as $showtime) { if ($showtime['start'] <= $current_time && $current_time <= $showtime['end']) { // we have a show on air $isOnAir = true; // output echo '<h3>', $showtime['host'], ' <span>is on air</span></h3>'; // break loop break; } } // without a show on air (see above) if (!$isOnAir) { echo '<h3>No live show is on</h3>'; foreach ($liveshows['mon'] as $showtime) { // find the first where start > current if ($current_time < $showtime['start']) { // output echo '<p>The next live show is at ', date('g:iA', strtotime($showtime['start'])), '</p>'; // and break loop break; } } } }