waffe
asked on
Date base of dates - what is the closest date to the current date?
Hi I am using a sort date function in PHP like this:
$result = mysql_query("select * from shows ORDER by showdate");
Now what I need to do is a comparison of the current date to the dates in my data base.
I need to know the number of the row that is closet to the current date. Like this
todays date = 08.04.04
Before sort:
month.day.year.
07.11.04
09.14.04
08.02.04
08.07.04
afrer sort:
07.11.04
08.03.04
08.07.04
09.14.04
The number of the row that is closest to the curret date is "2" (08.03.04)
I need this number so I can pass it on to a data grid so it will automatically select the date closest to the current date. Can this be done?
Thanks,
Waffe
$result = mysql_query("select * from shows ORDER by showdate");
Now what I need to do is a comparison of the current date to the dates in my data base.
I need to know the number of the row that is closet to the current date. Like this
todays date = 08.04.04
Before sort:
month.day.year.
07.11.04
09.14.04
08.02.04
08.07.04
afrer sort:
07.11.04
08.03.04
08.07.04
09.14.04
The number of the row that is closest to the curret date is "2" (08.03.04)
I need this number so I can pass it on to a data grid so it will automatically select the date closest to the current date. Can this be done?
Thanks,
Waffe
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SELECT id,MIN((UNIX_TIMESTAMP()-m
where the table looks like:
test(id*, timestamp)
The query returns the id of the tuple with the smallest difference in time.
N.B!. you will have to modify it to work 100%, e.g. how to react for future dates which now return a -number and so on. But I think this would be the way I would try to do it. You
You might also want to check out the MySQL function: DATEDIFF (http://dev.mysql.com/doc/mysql/en/Date_and_time_functions.html) but it only counts days...
Good luck,
//madwax
or