I am managing an internet radio station webpage and I would like to add an auto entry for the station's programming based on the day of week / time of day. For example at 8.00am on a Monday to Friday the "Now Playing" title shows "The Breakfast Show", at 10.00am it changes to "The Fred and Lucy Hour", and on a Sunday at 6.00pm it shows "The Hymn Hour"" etc. etc.
I have worked out how to get the server date and time on the webpage using PHP, and I am guessing this should somehow be linked to an SQL Database which containg all the shows and their timings, but as a complete novice on all things PHP and SQL I have no idea how to link them together (if indeed it is possible) or whether any other additional programming is necessary.
I would appreaciate a steer in the right direction from someone better qualified than me.
Thanks in advance.
CodePudding user response:
You can use simple code like this:
<?php
$weekday = date('N');
$hour = date('H');
if (1 <= $weekday && $weekday <= 5) {
// Monday to friday
if ( $hour > 8 ) {
$now_plaing = 'The Breakfast Show';
} elseif ( $hour >=10 ) {
$now_plaing = 'The Fred and Lucy Hour';
} else {
$now_plaing = 'Some good show';
}
} elseif ($weekday == 7) {
// Sunday
if ( $hour > 6 ) {
$now_plaing = 'The Hymn Hour';
} else {
$now_plaing = 'Sunday good show';
}
}
printf("Now Playing: %s", $now_plaing);
Another way is build shows array (or get it from Data Base) and use another code:
<?php
$shows = [
1 => [
0 => 'Morning show',
8 => 'The Breakfast Show',
10 => 'The Fred and Lucy Hour'
],
/*** each day shows array ***/
7 => [
0 => 'Sunday good show',
6 => 'The Hymn Hour'
]
];
$weekday = date('N');
$hour = date('H');
$now_plaing = 'Default show';
foreach($shows[$weekday] as $h=>$show) {
if ($h <= $hour) $now_plaing = $show;
}
printf("Now Playing: %s", $now_plaing);
Using DB:
<?php
$weekday = date('N');
$hour = date('H:i');
$now_plaing = 'Default show';
$query = "SELECT show_name FROM shows WHERE weekday = ? AND start_at <= ? ORDER BY start_at DESC LIMIT 1;";
$stmt = $pdo->prepare($query);
$stmt->execute([$weekday, $hour]);
$show = $stmt->fetch(PDO::FETCH_ASSOC);
printf("Now at %s playing: %s", $hour, $show['show_name']);