我正在管理一個網路廣播電臺網頁,我想根據星期幾/一天中的時間為該電臺的節目添加一個自動條目。例如,周一至周五上午 8 點,“正在播放”標題顯示“早餐秀”,上午 10 點更改為“弗雷德和露西時間”,周日下午 6 點顯示“贊美詩時間” ""等等等等。
我已經研究出如何使用 PHP 在網頁上獲取服務器日期和時間,我猜這應該以某種方式鏈接到包含所有節目及其時間的 SQL 資料庫,但作為 PHP 和SQL 我不知道如何將它們鏈接在一起(如果確實可能的話)或者是否需要任何其他額外的編程。
我會從比我更有資格的人那里獲得正確方向的指導。
提前致謝。
uj5u.com熱心網友回復:
您可以使用這樣的簡單代碼:
<?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);
在線測驗 PHP 代碼
另一種方法是構建顯示陣列(或從資料庫中獲取)并使用另一個代碼:
<?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);
PHP沙箱
使用資料庫:
<?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']);
SQL PHP 測驗
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/337077.html
