Click to See Complete Forum and Search --> : Date and Time in mySQL


pc_newbies
October 11th, 2005, 07:06 AM
Hi all,

How do I compare Dates and Times in mySQL?
Say I have an input in the form yy-mm-dd hh:mm (from a texbox), and I have 05-10-11 14:00 (DATETIME) in my database. What Query do I need to perform to determine whether the input is after or before the date in mySQL???

thanks heaps

wildfrog
October 11th, 2005, 07:34 AM
What Query do I need to perform to determine whether the input is after or before the date in mySQL???
A query like this works for me:
select * from table where date > '02-04-23 12:00' and date < '02-04-27 12:00'
- petter

ovidiucucu
October 11th, 2005, 10:33 AM
A litle bit safer and more flexible is to use DATE_FORMAT function (see more info about it in MySQL Reference Manual (http://dev.mysql.com/doc/)).
Here is an example:

SELECT ... FROM ... WHERE DATE_FORMAT(datetime_field,'%Y-%m-%d') < '2005-10-11'

NOTE: Avoid 2-digit year format... remember y2k problem.