概述: 大家好,我有一个非常小的PHP脚本,它试图通知浏览用户当前正在网络电台播放的内容以及下一个广播祷告将被播放的时间。现场祈祷时间为上午5:00至上午6:00,中午12:00至下午1:30以及晚上9:00至晚上10:30。当没有安排现场祈祷时,电台播放点播音乐和各种节目,如圣经教导等。星期六和星期日没有现场祈祷。有一个教堂服务在周日上午10:30到下午1:30之间流动。
问题 我已经成功地完成了大部分工作。该脚本正确显示当前事件名称。但是,它没有正确显示下一个祈祷时间。它总是返回下周一的第一个祈祷时间,只有在当前日期是星期五,星期六或星期日时才应该这样做。非常感谢您的帮助。
脚本:
$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;
?>
答案 0 :(得分:0)
我知道这不是一个答案,但评论中的许多代码行都非常难看。如有必要,可以在之后删除答案。话虽这么说,你需要进一步调查这个问题。假设下一个祷告存储在$next
中,请考虑以下方法
基本上,我在脚本中的可能位置添加了不同的字符串。运行它,看看哪一个被回显。显然,某处存在一些逻辑错误。
/**
* 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' . 'possibility1';
} elseif ($now >= $noon_start && $now <= $noon_end) {
$event = 'Prière de midi (EST)';
$next = date($next_dateformat) . ' 21:00:00' . 'possibility2';
} 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;
$next .= . 'possibility3';
} else {
$event = $detente;
$next = $next_monday_firstprayer . 'possibility4';
}
} elseif ($day == 6) { // $day is Saturday
$event = $detente;
$next = $next_monday_firstprayer . 'possibility5';
} // $day is Sunday
else {
$event = ($now >= $church_start && $now <= $church_end) ? 'Culte du dimanche' : $detente;
$next = $next_monday_firstprayer . 'possibility6';
}
$modx->setPlaceholder('onair', $event);
$modx->setPlaceholder('nextprayer', $next);
$output = $modx->getChunk('OnAir', array(
'event' => $event,
'next' => $next
));
return $output;
答案 1 :(得分:0)
看起来我通过重写我的if语句来解决这个问题:
/**
* Set the current event name based on the day of the week
*/
// $day is any from Monday to Friday
if ($day < 6) {
$event = $detente;
$next = ($day < 5) ? $tomorrow_firstprayer : $next_monday_firstprayer;
if ($now >= $morning_start && $now <= $morning_end) {
$event = 'Prière de 5h (EST)';
$next = date($next_dateformat) . ' 12:00:00';
}
if ($now >= $noon_start && $now <= $noon_end) {
$event = 'Prière de midi (EST)';
$next = date($next_dateformat) . ' 21:00:00';
}
if ($now >= $evening_start && $now <= $evening_end) {
$event = 'Prière de 21h (EST)';
}
} elseif ($day == 6) { // $day is Saturday
$event = $detente;
$next = $next_monday_firstprayer;
}
else { // $day is Sunday
$event = ($now >= $church_start && $now <= $church_end) ? 'Culte du dimanche' : $detente;
$next = $next_monday_firstprayer;
}
// Set Placeholders to use use anywhere in the template
$modx->setPlaceholder('onair', $event);
$modx->setPlaceholder('nextprayer', $next);
$output = $modx->getChunk('OnAir', array(
'event' => $event,
'next' => $next
));
return $output;