• Notice regarding image hosts and attachments. See here.

Php / MySQL date sort function

Associate
Joined
25 Aug 2004
Posts
163
Location
Reading
I have a php date variable (either as it is or as a timestamp) and I want to get the closest matching value to that date from the MySQL database, which there may be more than one of. Is there anyway of doing that in a query or do I have to pull everything in a 'select *' query and sort through the array and use my value as a pointer and select the above or below value? And I need to just select one date (or the set at that date rather) if there is the same distance between the date below and above the current date. It's bugging me now!

Cheers,

Gareth
 
For time the best thing to use it php's time() function (it does it in seconds) so basically you can then do < > =! etc and all the works =)


an example you want a record to be valid for 24 hours, you would have.

$validtime = time() + 86400;

then you can check if the time has passed, how long left etc by just compairing the times.

hope this helps

c0w
 
You can do it all in an SQL query (see link.) What happens when you have a million rows to sort through? Don't wanna be putting that into a huge array and sorting through it with PHP :)
 
That's true. I'm going for the query option for sure. I know there's not going to be more than a few thousand entries, still always nicer to have the results you want returned for php to handle as opposed to the whole table!
 
Back
Top Bottom