Log in

View Full Version : timer script to show image..



insanemonkey
02-26-2008, 08:29 PM
I have been looking for a script like this and cant seem to make one on my own...

what I need is a script that will change a image depending on the time of day.

say like from 12am-3pm display image and 3:01pm-11:59pm show image...

can someone give me a pointer.. and is it possible with php, I dont really want to use javascript for this...

its a for a website that I am building for a restaruant.. Thanks guys...

Medyman
02-26-2008, 08:31 PM
Do a search...

I've seen this request hundreds of times on this forum. There are scripts laying around. Specfically, try limiting your search to the request a script forum.

insanemonkey
02-26-2008, 08:41 PM
no the others are so different..

what I am trying to do is like..
if($time == $lunch) {
echo
}
if($time == $dinner) {
echo
}
can someone help on how to actually do this.. just a little bump please.... thanks

Medyman
02-26-2008, 08:51 PM
Oh I see...

That's easy enough...


<?

$time = date('H');

if($time <15) {
echo "Lunch";
}
else {
echo "Dinner";
}

?>

This will echo "Lunch" when it's between 0:00 (Midnight) and 15:00 (3:00 PM). Anything over that (i.e. 3:01 to 11:50 or 15:00 to 23:59) echos "Dinner"

insanemonkey
02-26-2008, 09:00 PM
thanks.. not very good with learning the greater then signs or less then signs.. still have to learn that part.. but thanks...!!

Leafy
02-27-2008, 01:26 AM
You could use this to display an image depending on the time.



<?
$time = date('H');
$images = array(
"Morning" => "morning.jpg",
"Noon" => "daytime.jpg",
"Night" => "nighttime.jpg"
);
if($time > 6 && $time < 10) {
// Morning
$img = $imagse["Morning"];
}
if($time >= 10 && $time < 20) {
// Day time
$img =$images["Noon"];
}
else {
// Night time
$img = $images["Night"];
}
Header("Location: $img");
?>

Twey
02-27-2008, 04:08 PM
<?Don't use short opening tags. They've been deprecated and may be disabled on the user's server.
Header("Location: $img");The Location header takes an absolute URI.
<?php
define('FROM_TIME', 0);
define('IMAGE_PATH', 1);

$times = array(
array(0, 'night.png'),
array(6, 'morning.png),
array(10, 'day.png'),
array(20, 'night.png'));

function getFloored($arr, $val) {
for ($i = 0; $i < count($arr); ++$i)
if ($arr[$i][0] > $val)
return $arr[$i - 1][1];
return false;
}

$t = getFloored($times, date('H') + 0);
$img = sprintf('<img src="%s" alt="%s">', $t = $t[IMAGE_PATH], basename($t, '.png'));
?>

Leafy
02-27-2008, 10:27 PM
Don't use short opening tags. They've been deprecated and may be disabled on the user's server.The Location header takes an absolute URI.
<?php

define('FROM_TIME', 0);
define('IMAGE_PATH', 1);

$times = array(
array(0, 'night.png'),
array(6, 'morning.png),
array(10, 'day.png'),
array(20, 'night.png'));

function getFloored($arr, $val) {
for ($i = 0; $i < count($arr); ++$i)
if ($arr[$i][0] > $val)
return $arr[$i - 1][1];
return false;
}

$img = sprintf('<img src="%s" alt="%s">', $t = getFloored($times, date('H') + 0), basename($t[1], '.png'));

If you want to nitpick,

You didn't close the php tag
The short opening tag is only there because I copied from the above script, I didn't write it completely from scratch.
The Location header shouldn't take an absolute URI, as I've used it without one and it works fine.
Why did you complicate the script so much? It's a simple enough script.

Twey
02-28-2008, 12:54 AM
If you want to nitpick,Nitpick?
You didn't close the php tagYou're right. My error.
The short opening tag is only there because I copied from the above script, I didn't write it completely from scratch.You were correcting it, right?
The Location header shouldn't take an absolute URI, as I've used it without one and it works fine.It should take an absolute URI, although most browsers nowadays will accept a relative URI. This is non-standard behaviour, and shouldn't be relied upon.
Why did you complicate the script so much? It's a simple enough script.It's not really more complicated -- I think it's shorter than yours, although I haven't counted the lines. It's just a more elegant way of approaching the problem, with less boilerplate.

Leafy
02-28-2008, 01:25 AM
Nitpick?

nitpick
v : be overly critical; criticize minor detai

Twey
02-28-2008, 03:10 AM
I'm aware of the definition of the term, I was questioning its application. Portability and standards incompliance are fairly big issues.