人氣 4562°c

每個月或星期顯示不同的內容或音樂

PHP或JS想根據每個月或每個星期或自定日子顯示(隱藏)不同的內容或按日期順序播放不同的網頁音樂(背景音樂)。

最近因有一個項目需要每個月按不同的日子顯示不同的內容,後來在國外的網站找來了一些PHP及javascript代碼。

編碼除了按每星期顯示不同的內容,還可以根據不同的日期播放不同網頁背景音樂,網站每一天都有自己喜愛的音樂,代碼用途非常之廣泛。

(1)每個月顯示不同的內容 PHP版
<?php
if( "1" === date( 'n' ) ) {
?>
1月顯示的內容
<?
}
if( "2" === date( 'n' ) ) {
?>
2月顯示的內容
<?
}
?>
<?
if( "3" === date( 'n' ) ) {
?>
3月顯示的內容
<?
}
?>
<?
if( "4" === date( 'n' ) ) {
?>
4月顯示的內容
<?
}
?>
<?
if( "5" === date( 'n' ) ) {
?>
5月顯示的內容
<?
}
?>
<?
if( "6" === date( 'n' ) ) {
?>
6月顯示的內容
<?
}
?>
<?
if( "7" === date( 'n' ) ) {
?>
7月顯示的內容
<?
}
?>
<?
if( "8" === date( 'n' ) ) {
?>
8月顯示的內容
<?
}
?>
<?
if( "9" === date( 'n' ) ) {
?>
9月顯示的內容
<?
}
?>
<?
if( "10" === date( 'n' ) ) {
?>
10月顯示的內容
<?
}
?>
<?
if( "11" === date( 'n' ) ) {
?>
11月顯示的內容
<?
}
?>
<?
if( "12" === date( 'n' ) ) {
?>
12月顯示的內容
<?
}
?>

(2)每個月顯示不同的內容 PHP版
<?php
// get today's date
$today = new DateTime();
// get the season dates
$m01 = new DateTime('January 01');
$m02 = new DateTime('February 01');
$m03 = new DateTime('March 01');
$m04 = new DateTime('April 01');
$m05 = new DateTime('May 01');
$m06 = new DateTime('June 01');
$m07 = new DateTime('July 01');
$m08 = new DateTime('August 01');
$m09 = new DateTime('September 01');
$m10 = new DateTime('October 01');
$m11 = new DateTime('November 01');
$m12 = new DateTime('December 01');
switch(true) {
case $today >= $m01 && $today < $m02:
echo '一月份顯示的內容';
break;
case $today >= $m02 && $today < $m03:
echo '二月份顯示的內容';
break;
case $today >= $m03 && $today < $m04:
echo '三月份顯示的內容';
break;
case $today >= $m04 && $today < $m05:
echo '四月份顯示的內容';
break;
case $today >= $m05 && $today < $m06:
echo '五月份顯示的內容';
break;
case $today >= $m06 && $today < $m07:
echo '六月份顯示的內容';
break;
case $today >= $m07 && $today < $m08:
echo '七月份顯示的內容';
break;
case $today >= $m08 && $today < $m09:
echo '八月份顯示的內容';
break;
case $today >= $m09 && $today < $m10:
echo '九月份顯示的內容';
break;
case $today >= $m10 && $today < $m11:
echo '十月份顯示的內容';
break;
case $today >= $m11 && $today < $m12:
echo '十一月份顯示的內容';
break;
default:
echo '十二月份顯示的內容';
}
?>

每星期顯示不同的內容 PHP版
<?php
if( date( 'w' ) == 0 ) {
?>
星期日-顯示的內容
<?php
}
if( date( 'w' ) == 1 ) {
?>
星期一-顯示的內容
<?php
}
?>
<?php
if( date( 'w' ) == 2 ) {
?>
星期二-顯示的內容
<?php
}
?>
<?php
if( date( 'w' ) == 3 ) {
?>
星期三-顯示的內容
<?php
}
?>
<?php
if( date( 'w' ) == 4 ) {
?>
星期四-顯示的內容
<?php
}
?>
<?php
if( date( 'w' ) == 5 ) {
?>
星期五-顯示的內容
<?php
}
?>
<?php
if( date( 'w' ) == 6 ) {
?>
星期六-顯示的內容
<?php
}
?>

備註:
Drop the am and pm and think in 24 hours.
if (date('w') == 1) {
if (date('H') >= 0 && date('H') < 17) {
// the thing you want on monday 0:00 (12 am) to 17:00 (5pm)
}
}

(1)自定義-以特定的日子顯示不同內容

例如:1號、15號、25號內容才會顯示出來。

<?php
if( date( 'd' ) == 1 ) {
?>
1號顯示的內容
<?
}
if( date( 'd' ) == 7 ) {
?>
7號顯示的內容
<?
}
?>
<?
if( date( 'd' ) == 15 ) {
?>
15號顯示的內容
<?
}
?>
<?
if( date( 'd' ) == 20 ) {
?>
20號顯示的內容
<?
}
?>
<?
if( date( 'd' ) == 25 ) {
?>
25號顯示的內容
<?
}
?>
<?
if( date( 'd' ) == 27 ) {
?>
27號顯示的內容
<?
}
?>
<?
if( date( 'd' ) == 30 ) {
?>
30號顯示的內容
<?
}
?>

(2)自定義-以特定的日子顯示不同內容
<?php if( date( 'd' ) == 3 ):?>
每月3號顯示的內容
<?php else: ?>
其餘日子顯示的內容
<?php endif;?>

<?php if( date( 'd' ) == 4 ) { ?>
每月4號顯示的內容
<?php } else if( date( 'd' ) == 8 ) { ?>
每月8號顯示的內容
<?php } else { ?>
其餘日子顯示的內容
<?php } ?>

<?php if( in_array(date( 'd' ), array(1,7,15,28)) ):?>
每月1,7,15,28號顯示的內容
<?php else: ?>
其餘日子顯示的內容
<?php endif;?>

<?php
if( in_array(date( 'd' ), array(20,21)) ) {
echo "20 and 21 content";
} elseif(in_array(date( 'd' ), array(22,23)) ) {
echo "22 and 23 content";
} else {
echo "Other content";
}
?>

以日期顯示不同的內容 JS版
<script language="javascript">
<!--
page=new Date(); //得到當前日期
if (page.getDate() == 1) //如果“日期”為1,則顯示此內容
document.write("1號內容");
if (page.getDate() == 2) /*內容順序代碼通過js寫入頁面*/
document.write("2號內容");
if (page.getDate() == 3)
document.write("3號內容");
if (page.getDate() == 4)
document.write("4號內容");
if (page.getDate() == 5)
document.write("5號內容");
if (page.getDate() == 6)
document.write("6號內容");
if (page.getDate() == 7)
document.write("7號內容");
if (page.getDate() == 8)
document.write("8號內容");
if (page.getDate() == 9)
document.write("9號內容");
if (page.getDate() == 10)
document.write("10號內容");
if (page.getDate() == 11)
document.write("11號內容");
if (page.getDate() == 12)
document.write("12號內容");
if (page.getDate() == 13)
document.write("13號內容");
if (page.getDate() == 14)
document.write("14號內容");
if (page.getDate() == 15)
document.write("15號內容");
if (page.getDate() == 16)
document.write("16號內容");
if (page.getDate() == 17)
document.write("17號內容");
if (page.getDate() == 18)
document.write("18號內容");
if (page.getDate() == 19)
document.write("19號內容");
if (page.getDate() == 20)
document.write("20號內容");
if (page.getDate() == 21)
document.write("21號內容");
if (page.getDate() == 22)
document.write("22號內容");
if (page.getDate() == 23)
document.write("23號內容");
if (page.getDate() == 24)
document.write("24號內容");
if (page.getDate() == 25)
document.write("25號內容");
if (page.getDate() == 26)
document.write("26號內容");
if (page.getDate() == 27)
document.write("27號內容");
if (page.getDate() == 28)
document.write("28號內容");
if (page.getDate() == 29)
document.write("29號內容");
if (page.getDate() == 30)
document.write("30號內容");
if (page.getDate() == 31)
document.write("31號內容");
page=new Date(); //取當前時間
var months = new Array(13); //建立月份描述的數組
months[1] = "1月";
months[2] = "2月";
months[3] = "3月";
months[4] = "4月";
months[5] = "5月";
months[6] = "6月";
months[7] = "7月";
months[8] = "8月";
months[9] = "9月";
months[10] = "10月";
months[11] = "11月";
months[12] = "12月";
var dateObj = new Date() //取當前時間
var lmonth = months[dateObj.getMonth() + 1] //得到月份值(如要符合習慣的月份值,則還需加一,因為js裏的月份是從0開始計算的)
var date = dateObj.getDate() //取當前星期描述
document.write("<br>" +"hi,你好!" +"<br>今天是"+ lmonth + date+"日,<br>"+"讓我們來聽聽今天的音樂") //寫問候語
//-->
</script>

PHP 更多日期時間格式及函數:查看!

標籤: ,