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; ?>;
}
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.I used something like this
your php:
in your html:
then just style your .night or .day classes
You can set different css files, one for day time and the other for the night.
Then, in the
<meta>
tag, that define the page's style file.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:
Hope this helps !
Try changing:
to:
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:
with: