Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.