Jump to content

Determine if date is in past or present / future


benjudy

Recommended Posts

OK, another one from the newbie files... Searching this forum as well as Googling the Web didn't turn up the simple help I'm looking for.

 

I've got MySQL fields named "started" and "ended". They are dates (data type "date"). I just want to detect whether the date is in the past, or if the date is today or a future date. I would think this would be easy but the answer has so far eluded me.

 

Is it possible to do this in the sql query? Something like:

 

$query_string = "SELECT * FROM $tablename WHERE ended='date in the past';

(Obviously 'date in the past' is my pseudocode for what I'm trying to do)

 

Is it possible to write a WHERE condition that's smart enough to know past from present or future? Or must this be done in PHP? If so, how?

 

I can provide full code if necessary, but this is very generic. I'm just looking for someone to point me in the right direction. Thanks!

Try:

 

$query_string = "SELECT * FROM $tablename WHERE UNIX_TIMESTAMP(`ended`) < UNIX_TIMESTAMP()"

 

The idea is that we convert our date to a unix timestamp (the number of seconds past since 1st january 1970), which we can then compare with the current timestamp.

 

See here for what can be achieved with date and time functions in mysql.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.