Forum Moderators: coopster

Message Too Old, No Replies

finding dates in the past, present and future

         

surrealillusions

7:07 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



Hi all..got a (possibly) complicated problem...

What i need to do is to find the months (in particular) of all 3 times (past, present and future). Hopefully this explains it best -

<current month>
some data pulled from a database to do with that month

<past months in order>
more data from the database, seperated by months...so..

June
some data
May
more data
April
even more data

etc...

Future months are not shown, but automatically appear when that month..uh..appears? so theres no need to manually update the script. And the current month will then go into the past months, and a new current month takes it place.

Editing the data in the database is not an ideal solution, although it has a date attached to it in the form of 0000-00-00 00:00:00 (year-m-day h:m:s).

Ive looked over the php date functions over at php.net and theres too many to get my head round all at once to decide which ones i need, and how to go about it. At the moment, im thinking all the months in an array, but then i get stuck how to find which one is the current, past etc...especially with differing years.

d40sithui

7:35 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



Sounds like you want to compare the dates. In the query just do an "ORDER BY date_field DESC" or "ORDER BY date_field ASC" and it should take care of itself. MySQL understands dates and you can compare them simply with using basic operators (<, >, =). Another is to like you say save the dates in an array. Use the strtotime() function to get the Unix timestamp of each date - which you then will be able to compare with each other using basic operators.