TAGS :Viewed: 10 - Published at: a few seconds ago

[ PHP select all where date is after now ]

I have a system with different classes and lectures that my users can sign up for. There is a last date for the sign up that date stored in the data base like: yyyy-mm-dd.

On the front page, I want to show when the last sign up for the next classes is. I want to be able to something like:

SELECT * FROM classes WHERE last_reg < now()

Answer 1


You almost had it:

SELECT * FROM classes WHERE last_reg > curdate()

You want to compare the last registration date with the current date and select what is not closed - or still available.

I flipped the less than to a greater than, otherwise you would only be selecting the items that were already closed.

Working with dates is no different to working with numbers - except that you get to use a bunch more date functions that are in MySQL already.

For example, with numbers, if you wanted to find anything less than 10 you would put in field<10 and that would be it, but if you want to use a date that is for example, more than ten days ago, you have to start using the date functions like this:

select * from SomeTable where dateField<date_sub(curdate, interval 10 day)