views:

70

answers:

1

Before i start id like to say ive posted this question as more of a discussion rather than Problem Question.

In my Database i have news posts lets say with 3 columns (Id, title, date). Wher Id and title are self Explanitory the date is stored in mktime() values, in other words the number of seconds passed since 1 January 1970.

Now what i want to do is build an archive link that will display as such

  • July 2009
  • June 2009
  • March 2009
  • Feburary 2009
  • December 2008

Note the months on which there were no posts are not displayed.

Now as an initial thought i was thinking

  1. Start with the last day of the current Month
  2. And get the Value of the First day of the current Month
  3. Do a MySQL COUNT Query/mysql_num_rows for posts that were date >= First_Day_Seconds AND date <= Last_Day_Seconds
  4. Display or put the values in an Array
  5. Do another Query to Check if Any more values are found WHERE date < First_Day_Seconds (break if no rows were found)

Now the above is just something on the top of my head. But if you got any ideas to speed this process up please share.

Will say in advance, date needs to be in mktime format

+3  A: 

I would suggest using a database "native" time format, but it works with UNIX timestamps as well.

You can simply do:

SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;

Optionally with a WHERE clause limiting the dates to past or future dates. Possibly an ORDER clause thrown in for good measure. That should be pretty much all that's needed, let the database do as much work as possible.

If you need more formatting options, select the dates with "%Y-%m" instead and format them in PHP:

date($myCustomFormat, strtotime("$date-01"));
deceze
Nicely Done... I just ran that Query and WOW!
Shahmir Javaid