Image Directory Used According to Time of Day

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
hungry_to_learn
Forum Newbie
Posts: 2
Joined: Sat Jan 01, 2011 1:09 pm

Image Directory Used According to Time of Day

Post by hungry_to_learn »

PHP noob here...
I want my website to display a light blue theme during daytime browsing, and a dark gey theme during night browsing. I have generic code to discriminate for time based display, but I'm stuggling with how to indicate how to choose the appropriate directory, say, "day" for day images, and "night" for night images.

Both sets of image files will be named verbatim, just reside in different directories, and be accessed during differrent times of the day.

How do I indicate which folder to pull files from based on the time variable of "$h"?

Any help would be greatly appreciated ;o)

***** PLEASE USE THE PHP CODE TAG *****

Code: Select all

<?php
$h = date('G'); //set variable $h to the hour of the day

if ($h < 7) $img = 'fish.gif';
elseif ($h < 17) $img = 'images/frog.gif';
else $img = 'potato.gif';

echo '<img src="',$img,'" width="200" height="200" border="0" alt="Time Senstive Picture Display" />'; //display the appropriate image according to timestamp
?>
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: Image Directory Used According to Time of Day

Post by Zyxist »

A part of code from my home page which uses the same trick:

Code: Select all

$time = time();	

$sunset = date_sunset($time, SUNFUNCS_RET_TIMESTAMP, $refLatitude, $refLongtitude, $refZenith, 0);
$sunrise = date_sunrise($time, SUNFUNCS_RET_TIMESTAMP, $refLatitude, $refLongtitude, $refZenith, 0);
		
if($time < $sunrise || $time > $sunset)
{
	$directoryPath = '/design/night/';
}
else
{
	$directoryPath = '/design/day/';
}

// ....

echo '<link rel="stylesheet" href="'.$directoryPath.'style.css" type="text/css" />';
echo '<img src="'.$directoryPath.'image.png" alt="foo" />';
hungry_to_learn
Forum Newbie
Posts: 2
Joined: Sat Jan 01, 2011 1:09 pm

Re: Image Directory Used According to Time of Day

Post by hungry_to_learn »

Thank you so much for your response - I really appreaciate your help!
Post Reply