Switching Pics with the Date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sake
    New Member
    • Jan 2007
    • 46

    Switching Pics with the Date

    I'm doing a little project for a site, and I needed to rotate a picture every day. I think I have most of the code planned out in my head, but I'm sorta stuck on how to tell when its a new day. Should I just store a date in my database, and if it doesn't match the current date, switch the picture? If I did that, how would I go about doing it? Is there a more efficient/easier way?

    Thanks Alot,
    Sake
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    Originally posted by sake
    I'm doing a little project for a site, and I needed to rotate a picture every day. I think I have most of the code planned out in my head, but I'm sorta stuck on how to tell when its a new day. Should I just store a date in my database, and if it doesn't match the current date, switch the picture? If I did that, how would I go about doing it? Is there a more efficient/easier way?

    Thanks Alot,
    Sake
    I would create a table called DailyPictures with fields media_id and display_date. Have media_id point to your media table, and have display_date initially be null. on any given day, select from the table where date is today. If no results are returned, select a random entry where the date is NULL and update that entry with the current date. If that select returns no rows, select the entry with the lowest date, and update that date to today.

    What is nice about this scheme is that a) it allows you to build a list of images from your existing media table b) it will never run out of images (it will loop if there are no images that have not been previously shown) and c)it is rather simple to implement, and maintains the history of your picture of the day.

    Comment

    • sake
      New Member
      • Jan 2007
      • 46

      #3
      Thanks, that sounds really ingenious actually :)

      Just one other question though: How can I match dates from PHP and the server? I made the DATE column of type date, but Im not sure how to get the current date in the same format in PHP. I should use the date() function right?

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Originally posted by sake
        Thanks, that sounds really ingenious actually :)

        Just one other question though: How can I match dates from PHP and the server? I made the DATE column of type date, but Im not sure how to get the current date in the same format in PHP. I should use the date() function right?
        Yes, or, if you are using MySQL as your database, you could just compare with CURDATE().

        Comment

        Working...