Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Background for Night & Day using php?

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; ?>;
}
like image 820
Josh Tha CreativeOne Avatar asked Dec 05 '10 06:12

Josh Tha CreativeOne


People also ask

How do I change the background of a picture every hour?

Under “Change picture every,” click the box and select “6 hours” if you have four background images you want to cycle through. You can also select “1 hour” here if you want to provide 24 background images. Ensure the Shuffle option is turned off.

How to change the background scene of a photo?

So, here is a way for the rest of humanity to change the background scene of any photograph – the PhotoScissors online tool. Step 1: Select the photo you want to edit. Open PhotoScissors online, click the Upload button then select an image file. The program will remove the background from an image and display it in the browser:

How do I change the sunset/sunrise times of the dynamic wallpaper?

You can change the dynamic wallpaper or its sunset/sunrise times anytime. To do so, select the Configure Schedule or Select Theme options on the WinDynamicDesktop context menu. Then adjust the settings from those windows as required.

How do I change the background of my Windows 10 computer?

In the Desktop Background window, click the “Browse” button to the right of Picture location, and then choose the folder containing your wallpaper images. Select the background images you want to use, and then enable the timed slideshow feature on the “Change Picture Every” dropdown menu.


2 Answers

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 !

like image 186
Valentin Flachsel Avatar answered Oct 30 '22 04:10

Valentin Flachsel


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';
}
like image 37
Stoic Avatar answered Oct 30 '22 02:10

Stoic