Hi there, please someone suggest a php code for changing site background image/color in every 30 minutes... I have 10 images 1.jpg to 10.jpg in www/images folder.

I have....

body {
    .... ... ...
        ... ... ...
    background: .... url('background.php') 50% 0 fixed;
    background-color: .....;
}

in style.css

what will be my php code in [B]background.php[/B].

I know some code which will change images randomly when reloading pages. But I need to change every 30 minutes...

background: #0facea url(back.php) no-repeat;
... ... ...
... ... ...
... ... ...
}

The code for back.php is :

<?php

header("Content-type: image/jpeg");
header("location:".'pictures/pictures_'.rand(1,6).'.jpg');
?>

Plz anyone reply...

Recommended Answers

All 3 Replies

Member Avatar for diafol

If you want to use CSS as opposed to a <img> tag, set a

class="<?php echo $class;?>"

on the body tag.

When you check the system time, calculate the half hours from midnight:

00:00-00:29 = 0
00:30-00:59 = 1

this can be done with a switch or multiple if/elseif

(I think you'll have 48-ish ranges). These values (0,1...) etc can then be used to set the classname in the body tag:

<body class="time_<?php echo $timevalue;?>">

Then your CSS can be something like:

body{
   /*default positions, sizes etc*/
}

.time_0{
   background-image: url('background1231.png');
   background-color: white;
}
.time_1{
   background-image: url('background4638.png');
   background-color: red;
}
..etc...

Just an idea. I have to say, I'd avoid this myself as it could confuse the hell out of an user - personally, I wouldn't like to peruse a site that was constantly changing . A casual browser may look at your site, think mmm that's useful and then try to come back to it, but doesn't recognize the page, so goes away dumbfounded. What about your 'identity' and feel? Colour schemes are extremely important - keep changing them at your peril.

An alternative solution: use filectime(), each file saves 3 different dates: created, modified and access, with touch() or file_put_contents() you can change "modified" date without changing contents. Here is an example:

<?php
$a = glob('*.jpg', GLOB_NOSORT);
$b = array();
print_r($a); # all images
for($i = 0; $i < count($a); $i++)
{
    $time = filectime($a[$i]);
    $b[$time] = $a[$i];
}

$mx = max(array_keys($b));
$mn = min(array_keys($b));

if((time() - $mx) < 3) # at moment 3 seconds, 1800 = 30 minutes
{
    echo 'visible: ' . $b[$mx] . "\n";
}
else
{
    echo 'new: ' . $b[$mn];
    touch($b[$mn]); # update "inode change time"
}
echo "\n";
?>

In order to work you need to change the "modified" date to each image, you can run this script right before you start the above one:

<?php
$a = glob('*.jpg',GLOB_NOSORT); # remove second parameter to sort files
for($i = 0; $i < count($a); $i++)
{
     touch($a[$i]);
     sleep(2);
}
?>

After this, if you don't modify the images there will be always the same order. Every time the page is loaded the script will check for the latest updated "modified" date and if the difference between this value and the current time is higher than 30 minutes (3 seconds in my example) it will update the file with the oldest time, and so on, in loop.
bye :)

Member Avatar for diafol

JUst a thought wrt to CSS - leave the body tag with default stuff - and have the following:

//check time
have an array of images (48-ish or duplicates):

$imgs = array('img138.png','img42gh.png,...);
$month = date('n');
$year = date('Y');
$day = date('j');

$timesecs = time() - mktime(0,0,0,$month,$day,$year);
$timevalue = floor($timesecs/1800); //to get value for 30min intervals
$img = $imgs[$timevalue];

then in head area AFTER the css file reference

<style>
  body{
    background-image: url(images/<?php echo $img;?>);
  }
</style>
commented: really great, this can help to display desired images at defined hours +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.