Reputation: 59585
Background:
I have a database table filled with over 500 rows
. The data is from a websites json
API. There is a column called created_at
which mimics the json
API not the local row created_at
time. Here is an example of the date / time format from the API 2009-12-10T20:08:00+0000
. The column in the database has the type varchar
which may be problematic later on.
Problem: I to wish setup a specific query or if a query cannot be achieved run all of the database rows through a loop. I want the ability to query posts at set date intervals, for instance, todays date is Aug 28th 2011, I want the weekly, monthly, and yearly posts that match that date.
Weekly would look like this (ie. Aug 28th 2011, Aug 21th 2011, Aug 14th 2011, Aug 07th 2011 ).
Monthly would look like this (ie. Aug 28th 2011, Jul 28th 2011,Jun 28th 2011, May 28th 2011 ).
Yearly would look like this (ie. Aug 28th 2011, Aug 28th 2010, Aug 28th 2009 ).
Special cases If one of these is not present I would like the ability to retrieve whichever preceding or proceeding row date is closer to the date requested. If their are multiple rows for one date I just want the first one.
Any help would be greatly appreciated.
UPDATE
I have added a column with the type datetime
and have pulled and updated the rows with the new created_at_datetime
column which correspondes to date('Y-m-d H:i:s', $jsonDate)
. I still have little idea how to form the mysql query.
Upvotes: 0
Views: 175
Reputation: 783
If you have to convert the date, use strftime(), http://php.net/manual/en/function.strftime.php
Use srttotime() to calculate dates in the past/future; get the previous month and last day of the month, then add a 24:59:59 signaling the last possible second of the month, and a 00:00:00 for the 1st possible second.
To get only 1 result in the database call, use LIMIT 1, and order by asc/desc.
Upvotes: 1