/    Sign up×
Community /Pin to ProfileBookmark

Image Depending On The Day

I was wondering if I could get a script that would change an image depending on the date. I don’t want it to run off the users local clock on their computer, because if their clock is wrong, they will see the wrong image. The image I wan’t this to affect is my logo image, It would be like google when some holiday is happening. If thier is no speicific date given, than I wan’t it to display the default image. This also has a mouseover affect, so when the user mouses over the image the image changes.

here is the image I want it to affect. it is the top one that says Dtop. Move your mouse over it. This is the image I wan’t to be affected.
[url]http://d-top.org[/url]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@SpectreReturnsDec 26.2005 — Use date() to determine the date, and then you can use GD to generate images based on that.
Copy linkTweet thisAlerts:
@bokehDec 26.2005 — First thing I would do is get rid of the javascript rollover and use CSS instead. Personally I would use a conjoined image so no preload is needed.

Once you have that sorted there are two ways you can do this: either use PHP to create the image url in your HTML/CSS or have PHP act as a servlet and serve the correct image. That way the image url would remain static but the image would change according to the logic in the image script.

What Spectre says above is right, although if you already have the images prepared you wont need GD.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorDec 26.2005 — OH, OK, I don't know how to do any of that. I know no PHP.
Copy linkTweet thisAlerts:
@HuevoosDec 27.2005 — OK here is my simple approach.

Disclaimer ?

Im going to write the following code on the fly and without any testing it may have some typos, but shall give you an example:

[code=php]
<?php

function nicelogo(){
//Default logo

$logo = "http://images.yourdomain.com/logos/logo.png";

$twodays = 172800; //there are 172800 seconds in 2 days (60 * 60 * 48)

//setting the desired holidays

$christmas = mktime ( 0, 0, 0, 12, 24, date(Y));

$halloween = mktime ( 0, 0, 0, 10, 31, date(Y));

$new_year = mktime ( 0, 0, 0, 12, 31, date(Y));

$my_birthday = mktime ( 0, 0, 0, 04, 06, date(Y));// ;)

//let's make a nice array (to keep simplicity I decided to put the image source in this array)

$holidays = array("xmas_logo.png" => $christmas, "scary_logo.png" => $halloween, "ny_logo.png" => $new_year, "huevoos_logo.png" => $my_birthday);

//let's loop

foreach($holidays as $img => $start_day){

$ending_day = $start_day + $twodays; //I'm not sure if that'll make a sum, but is the idea

if($start_day <= time() && time() <= $ending_day)
$logo = "http://images.yourdomain.com/logos/$img";

}

return $logo;
}
?>
[/code]


in your html you'll obviously have to put something like
[code=html]
<img src="<? echo $logo ?>" alt="my logo" title="my logo"/>
[/code]


questions, sugestions???
Copy linkTweet thisAlerts:
@SpectreReturnsDec 28.2005 — RE:Huevoos:

I'm just going to rip your script and modify it a bit so it's more clean (and will work).

[code=php]
<?php

function logo() {
$_[0] = "http://images.yourdomain.com/logos/";
$_[1] = "logo.png";

// $table is an array of 'mmdd' => 'reletive logo'
$table = array(
"0810" => "specs_birthday.png",
"1031" => "halloween.png",
"1225" => "christmas.png",
"1231" => "newyears.png"
);

$date = date("mj");

return ($_[0] . (in_array($date, $table) ? $table[$date] : $_[1]));
}

?>
[/code]


And to use it you would:

[code=html]<img src="<?= logo() ?>" alt="my logo" title="my logo" />[/code]


This could be extended to different images per year with the following:

[code=php]
<?php

function logo() {
$_[0] = "http://images.yourdomain.com/logos/";
$_[1] = "logo.png";

// $table is an array of 'yymmdd' => 'reletive logo'
$table = array(
"060810" => "specs_birthday.png",
"061031" => "halloween.png",
"061225" => "christmas.png",
"061231" => "newyears.png"
);

$date = date("ymj");

return ($_[0] . (in_array($date, $table) ? $table[$date] : $_[1]));
}

?>
[/code]
Copy linkTweet thisAlerts:
@HuevoosDec 28.2005 — The only eh... "mistake" i see in your code is that you can't specify for how long the custom logo is going to be there.

For instance new year is 1231 and 0101 or what about you think you spent a lot of time designing your shiny happy logo for people to see it just for one day???

Also is hard for newbies to understand your code,I think you should use the standard if(){}else{} syntax.

BTW sorry, for mine to work you have to put

[code=html]<img src="<? echo nicelogo() ?>" alt="my logo" title="my logo" />[/code]

not

[code=html]<img src="<? echo $logo ?>" alt="my logo" title="my logo" /> [/code]

little typo... i knew there was one

Cheers
Copy linkTweet thisAlerts:
@cwilkeyDec 28.2005 — You could also store your images in a database and call them according to the current date.
×

Success!

Help @The_Little_Guy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...