mercredi 25 novembre 2015

How to return the correct event name based on current time

Overview: Hi guys, I have a very small PHP script that's attempting to inform the browsing user what's currently playing on the web radio and when the next radio prayer will be streamed. The live prayer times are 5:00 AM - 6:00 AM, 12:00 PM - 1:30 PM and 9:00 PM to 10:30 PM. When no live prayers are scheduled, the radio plays on-demand music and various programs, such as Bible teachings, etc. There are no live prayers on Saturdays and Sundays. There's a Church service that streams live on Sunday between 10:30 AM and 1:30 PM.

The issue I've managed to get most of it working. The script is correctly displaying the current event name. However, It doesn't correctly display the next prayer time. It's always returning Next Monday's first prayer time, which it should only do if the current date is Friday, Saturday or Sunday. Thanks already for your help.

The script:

    $output = '';

    // Set default timezone to Toronto
    date_default_timezone_set('America/Toronto');

    /**
     * Define radio prayer times in 24-hour format
     */
    $morning_start = strtotime('05:00');
    $morning_end   = strtotime('06:00');
    $noon_start    = strtotime('12:00');
    $noon_end      = strtotime('12:50');
    $evening_start = strtotime('21:00');
    $evening_end   = strtotime('22:30');
    // Sunday service times
    $church_start  = strtotime('10:30');
    $church_end    = strtotime('13:30');

    /** 
     * Set global date and time variables
     */
    // Current time in 24-hour format
    $now                     = strtotime(date('H:i'));
    // Current day of the week (1-7)
    $day                     = date('N');
    // Set the date format for the next event (after the current) 
    // This is used by a JQuery countown script -- output example: 2001/03/10
    $next_dateformat         = 'Y/m/d';
    $firstprayer             = ' 05:00:00';
    $tomorrow                = strtotime("tomorrow");
    $tomorrow_firstprayer    = date($next_dateformat, $tomorrow) . $firstprayer;
    $next_monday             = strtotime('next monday');
    $next_monday_firstprayer = date($next_dateformat, $next_monday) . $firstprayer;

    /**
    * Define the default event name when no radio prayer is scheduled
    */
    $detente = 'Détente Musique & Enseignements';

    /** 
     * Set the current event name based on the day of the week
     */
    // $day is any from Monday to Friday
    if ($day < 6) {
        if ($now >= $morning_start && $now <= $morning_end) {
            $event = 'Prière de 5h (EST)';
            $next  = date($next_dateformat) . ' 12:00:00';
        } elseif ($now >= $noon_start && $now <= $noon_end) {
            $event = 'Prière de midi (EST)';
            $next  = date($next_dateformat) . ' 21:00:00';
        } elseif ($now >= $evening_start && $now <= $evening_end) {
            $event = 'Prière de 21h (EST)';
            // Set next day's first prayer time. If $day is Monday - Thursday, return Friday 5:00 AM.
            // Else return Monday 5:00 AM since there are no radio prayers on Saturday or Sunday
            $next  = ($day < 5) ? $tomorrow_firstprayer : $next_monday_firstprayer;
        } else {
            $event = $detente;
            $next  = $next_monday_firstprayer;
        }
    } elseif ($day == 6) { // $day is Saturday
        $event = $detente;
        $next  = $next_monday_firstprayer;
    } // $day is Sunday
    else {
        $event = ($now >= $church_start && $now <= $church_end) ? 'Culte du dimanche' : $detente;
        $next  = $next_monday_firstprayer;
    }
    $modx->setPlaceholder('onair', $event);
    $modx->setPlaceholder('nextprayer', $next);

    $output = $modx->getChunk('OnAir', array(
        'event' => $event,
        'next' => $next
    ));

    return $output;
    ?>

Aucun commentaire:

Enregistrer un commentaire