Forum Moderators: coopster

Message Too Old, No Replies

Time related script

need a little help

         

Vlad

3:05 am on Nov 29, 2005 (gmt 0)

10+ Year Member



Hi guys, I'm new to php but already full of ideas...

I'm thinking of making a script that will rotate banners every hour.

The way I see it is:


get time;
if first number (hour)is positive ie. -->12:00
banner one
else
banner two

Hey don't laugh!
This is how I see it but can't code it yet.
Do you think it's doable?

My buddy recommended PHP unleashed, would that be a good book to start with for a newbie?

So far I have read a few tutorials online and got really interested.

Thanks for your help!

IamStang

4:48 am on Nov 29, 2005 (gmt 0)

10+ Year Member



I did something very similar to the following code on a buddy's site. Might not be the best way, but it works.

<?PHP
//reduce to 12 links for 12 hour
$url = array('http://link1.com', 'http://link2.com',
'http://link3.com', 'http://link4.com', 'http://link5.com',
'http://link6.com', 'http://link7.com', 'http://link8.com',
'http://link9.com', 'http://link10.com', 'http://link11.com',
'http://link12.com', 'http://link13.com', 'http://link14.com',
'http://link15.com', 'http://link16.com', 'http://link17.com',
'http://link18.com', 'http://link19.com', 'http://link20.com',
'http://link21.com', 'http://link22.com', 'http://link23.com',
'http://link24.com');

//reduce to 12 images for 12 hour
$img = array('img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg',
'img5.jpg', 'img6.jpg', 'img7.jpg', 'img8.jpg', 'img9.jpg',
'img10.jpg', 'img11.jpg', 'img12.jpg', 'img13.jpg', 'img14.jpg',
'img15.jpg', 'img16.jpg', 'img17.jpg', 'img18.jpg', 'img19.jpg',
'img20.jpg', 'img21.jpg', 'img22.jpg', 'img23.jpg', 'img24,jpg');

//could also do another array for alt text
//and another array for text link under banner

// 0-23 (24 hour format)
$hr = date(G);

// 1-12 (12 hour format)
//$hr = date(g);

$x=0;

while($x <= $hr){

if($x == $hr){
echo "<a href=$url[$x]><img src=path/to/pics/$img[$x]></a>";
$x++;
}//close if x
else {
$x++;
}//close else

}//close while
?>

Hope it helps.

As for your question on books, maybe someone else can help you there. My suggestion is to have a look at what your local library has to offer before comitting to a single book. Our library system has well over 100 books on php alone. I grab a few, read em and go get a few more, etc, etc. LOL

willybfriendly

5:13 am on Nov 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if(date(g)% 2)
echo $banner1;
else
echo $banner2;

Explanation: We are using the modulus and a 12 hour clock. Even numbers will leave a modulus of 0. Odd a modulus of 1.

Thus, even numbers return false and odd return true.

WBF

Vlad

9:21 pm on Nov 29, 2005 (gmt 0)

10+ Year Member



Thank you guys, just what I was looking for!

Edit/ um Willy, I'm trying your code now and get this error
Notice: Use of undefined constant g - assumed 'g' in C:\...\test.php on line 20

here is the code
<?
if(date(g)% 2)
echo "banner1";
else
echo "banner2";
?>

Any idea? Thanks again.

Vlad

9:36 pm on Nov 29, 2005 (gmt 0)

10+ Year Member



Never mind I got it now.
working code:

<?
if(date('g')% 2)
echo "banner1";
else
echo "banner2";
?>