可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm working on a tumblr page. I have two different backgrounds for the html page and I want the DAY background to display from 7am to 8pm and the night background to display from 8pm to 7am.
I decided to do this in php, but i'm a total newb when it comes to php. My friend sent me an example code that I could use. But I have no idea what to do with it. Can you guys help.
Here's the example code.
<?php
header('content-type: text/css');
date_default_timezone_set("America/Vancouver");
$now = date('G');
if ($now < 12) {
$bg = '#FFFFFF';
} else {
$bg = '#000000';
}
?>
body {
background: <?php echo $bg; ?>;
}
回答1:
The date()
will return the server local date/time, which means that if your server is in the US, a user from Eastern Asia or Australia for example, will see the wrong background.
I know this isn't what you were looking for, but I would suggest doing it with JS, which runs user-side rather than server-side, and therefore the timezone will not matter because the script will get the user's time, not the server's time. The script would look something like:
var localDate = new Date();
if (localDate.getHours() > 7 && localDate.getHours() < 20) {
document.bgColor = "#fff";
} else {
document.bgColor = "#000";
}
Hope this helps !
回答2:
Try changing:
body {
background: <?php echo $bg; ?>;
}
to:
<body style="background-color: <?php echo $bg; ?> !important;">
You PHP Code seems to be fine to me. Note that, it will display white background from 12:00 noon to 12:00 midnight. Try the following code for keeping white background from 7:00 AM to 8:00 PM and black other times (as Aaron said).
Replace:
if ($now < 12) {
$bg = '#FFFFFF';
} else {
$bg = '#000000';
}
with:
if ($now < 7 || $now > 20) {
$bg = '#000000';
} else {
$bg = '#FFFFFF';
}
回答3:
The date
function you're using is returning the 24 hour format of the current hour. Next, in the conditional you should modify that to check what range you're in. If $now
is between 7 and 20 you can set it to the daytime background. Otherwise, set it to the night time. Let me know if you need more details for doing this.
<?php
header('content-type: text/css');
date_default_timezone_set("America/Vancouver");
$now = date('G');
if ($now > 7 && $now < 20) {
$bg = '#FFFFFF';
} else {
$bg = '#000000';
}
?>
<body style="background-color:<?php echo $bg; ?> !important;">
<!-- Your HTML code here -->
</body>
回答4:
I used something like this
your php:
function getUKTime() {
date_default_timezone_set('Europe/London');
$time = date('H');
$style = "";
if ($time >= 6 && $time <= 18) {
$style = $style . "day";
}
else {
$style = $style . "night";
}
echo $style;
}
in your html:
<div id="uk" class="<?php getUKTime(); ?>">
<h1>U.K.</h1>
</div>
then just style your .night or .day classes
回答5:
You can set different css files, one for day time and the other for the night.
$date = getDate();
// check day or night
// it's day by default
$css = "css/main.css";
if ($date['hours'] < 7 || $date['hours'] > 20){
// is night
$css = "css/main-night.css";
}
Then, in the <meta>
tag, that define the page's style file.
<link href="<?= $css ?>" rel="stylesheet">