SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: dates in PHP .

  1. #1
    psycho
    Join Date
    May 2000
    Location
    London
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a list drawn from mysql ordered alphabetically. i want a "new" image to appear to ones added in the last week.

    they are submitted via a form. there is a column called "dateadded" which holds the date added in a YEAR-MONTH-DAY format.

    how can i do an if query which checks if this date is 1 week or less than the current date, and then displays an image if it is ?

    thanks
    james

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need to use the MySQL IF function along with the TO_DAYS function:

    Code:
    SELECT ..., IF((TO_DAYS(CURDATE())-TO_DAYS(datedadded)) < 7,1,0) AS is_new FROM ...
    This will create a column called is_new in your set of results, which will have a value of 1 for records less than 7 days old, and 0 for other records. You can then use this value in your PHP script to indicate whether or not a "NEW" image should be displayed.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    psycho
    Join Date
    May 2000
    Location
    London
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks ! worked to good effect !

    http://www.hiphopmusic.co.uk/reviews

    thanks again
    james


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •