Page 1 of 1

Need help with creating function that writes based on date

Posted: Tue Jul 27, 2010 8:09 am
by tf22raptor
Hello all, I am trying to create a peice of code in php that will write a line of code to a webpage based on what the date is.

eg: I have an image for every week of the year and I need to display that based on what week we are in in this year.

I guess this peice of code needs to be able to get todays date, determine if it is outside of the range of the week and then write the appropriate image.

Hoping someone might able able to help me come up with something, I'm only new to php but have asp experience.

I mean I guess I could write a whole bunch of if statements but then I think thats not gonna cut it.

Any help would be great

Re: Need help with creating function that writes based on da

Posted: Tue Jul 27, 2010 8:19 am
by Benjamin
You can store the dates to images map in a database or key value array.

Code: Select all

$foo = array(
    '2010-07-27' => 'image1.jpg',
    '2010-07-26' => 'image2.jpg',
);
Then to display the correct image, you access the file name using the key, which in this case is the date.

Code: Select all

<img src="images/<?php echo $foo[date("Y-m-d")]; ?>" />

Re: Need help with creating function that writes based on da

Posted: Tue Jul 27, 2010 10:02 am
by tf22raptor
Hey Benjamin thanks for the reply. What about if my images/content where now in a list, in an order and I had to keep them in this order and when I got to a certain week of the year/todays date my selector would go to a specific number in the list.

I guess I'm trying to put my selector at a specific place in the list based on what date it is.

Eg.

image1 (week1)
Image2 (week2)
Todays date is week 3
Image3
Image4
Image5

Under this case my selector would start at week3 and all of the values behind it I can use, the value before it are no longer needed.

You know what I mean, sort of hard to articulate. Is this iterating through an array based on date, is that what you would call it? How would I do that?

Re: Need help with creating function that writes based on da

Posted: Tue Jul 27, 2010 10:07 am
by Benjamin
Go to php.net and use the search form to search for date. The date function allows you to return the week of the year as an integer. If the image changes once per week you can still use my example.

Re: Need help with creating function that writes based on da

Posted: Tue Jul 27, 2010 9:24 pm
by tf22raptor
G'day Benjamin I have started another thread as I don't think I really gave enough info in my initial question. Take a peek if you get 5mins.

My how to position through an array based on date here:
viewtopic.php?f=1&t=119243&p=617598#p617598

Thanks for the responses.