«

»

Mar 03

Mysql – check if a date is between two dates

We need to check if an entered discount is valid. Valid condition is performed when the discount is active. Active is when the date the user enter the data is between start date discount and end date.

The simple table to save instance discount is “instance”

| Code | StartDate | EndDate |

  • code is a mysql varchar field
  • StartDate and EndDate are mysql date field

Just write down the query:

SELECT * FROM instance as i
WHERE CURDATE()>=StartDate AND
CURDATE()<=EndDate

with this query we know if there is come valid discount today(the date the user is entering data)

CURDATE() is a mysql special function to get the current date as a value in ‘YYYY-MM-DD’

2 comments

  1. Shabi

    Excellent man. Solved my problem. I was searching for this from a long time.

  2. chinux

    My problem is not limited to dates that are within range. If I also need, crossing dates that range.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>