2013-04-30 63 views
0

使用PHP,我将如何让我的背景图像在我的网站改变基于当前月份上?背景图基于当前月

月= 01.JPG

Febuary = 02.JPG

等....

目前我使用一个单独的目录 “rotator.php” 来获得不同的图像与每刷新一次。

CSS文件我有这样的:

{

背景:网址(../图像/主题/背景/ rotator.php);

这显然链接到rotator.php ....我想替换的index.php的rotator.php但我不知道用什么代码来得到它调出图像。

这是我到目前为止....

<?php 


// Image File Ext. 

$extList = array(); 
$extList['gif'] = 'image/gif'; 
$extList['jpg'] = 'image/jpeg'; 
$extList['jpeg'] = 'image/jpeg'; 
$extList['png'] = 'image/png'; 

//today and Image to use per today 

$today = date('m'); 
printf('$today'); 

if ($today == 01) { 
    $image = "01.jpg"; 
} 
elseif ($today == 02) { 
    $image = "02.jpg"; 
} 
elseif ($today == 03) { 
    $image = "03.jpg"; 
} 
elseif ($today == 04) { 
    $image = "04.jpg"; 
} 
elseif ($today == 05) { 
    $image = "05.jpg"; 
} 
elseif ($today == 06) { 
    $image = "06.jpg"; 
} 
elseif ($today == 07) { 
    $image = "07.jpg"; 
} 
elseif ($today == 08) { 
    $image = "08.jpg"; 
} 
elseif ($today == 09) { 
    $image = "09.jpg"; 
} 
elseif ($today == 10) { 
    $image = "10.jpg"; 
} 
elseif ($today == 11) { 
    $image = "11.jpg"; 
} 
elseif($today == 12) { 
    $image = "12.jpg"; 
} 

?> 

不知道这是在正确的方向前进....

+1

为什么你有一个java标签? – 2013-04-30 18:58:58

+0

许多不同的可能的解决方案。你有迄今为止尝试过的代码样本吗? – brbcoding 2013-04-30 18:58:59

+0

**提示**:'$月=日期( “M”);' – Aquillo 2013-04-30 18:59:31

回答

0

它可以用简单的事情要做:

printf('<html class="month-%s">', date('m')); 

并在CSS则:

body {background: /* default background style */} 
    .month-01 body {background-image: url('01.jpg')} 
    .month-02 body {background-image: url('02.jpg')} 
    ... 

这使您不仅可以根据月份对网站的身体进行设计,还可以对网站上的其他任何元素进行设计。

+0

检查我上面尝试的代码...让我知道我错在哪里....我想我需要一些东西来得到这个月...... – 2013-04-30 19:58:58

+0

@ C.Smith:为什么这么复杂?你不能改变HTML的类属性吗?这是更有效的(你可能想长期缓存你的CSS)。 – 2013-04-30 20:10:45

+0

使用简单机器论坛作为我的基础,只是试图操纵已经存在的内容以获得期望的效果,您在上面看到的文件就是我自己尝试的内容......我将css文件导入了系统主题寻找上面的文件...让我控制所创建的PHP所需的背景....我认为...lol – 2013-04-30 20:21:48

0

不知道你有你自己建立的,但我想到的最简单的方法是将PHP文件链接为您的样式表

<link rel="stylesheet" type="text/css" href="styles.php" /> 

而且在styles.php

<?php 
header("Content-type: text/css"); 
?> 
body { 
    background: url('<?= date('m') ?>.jpg'); // 'm' is number month (01 through 12) 
} 

干杯!

+0

我有index.css链接到图像目录中的rotator.php ....将在目录中使用index.php,使其选择正确的图像.... – 2013-04-30 19:19:54

+0

你的意思是你有这样的事情? 'background:url('rotator.php');' 如果是这样,您需要使用rotator.php为您要使用的图片格式传递正确的标题。 – kenhkelly 2013-04-30 19:24:49

+0

body { background:url(../ images/theme/backdrop/rotator.php); – 2013-04-30 19:32:11