benjudy Posted November 2, 2007 Share Posted November 2, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/75840-determine-if-date-is-in-past-or-present-future/ Share on other sites More sharing options...
GingerRobot Posted November 2, 2007 Share Posted November 2, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/75840-determine-if-date-is-in-past-or-present-future/#findComment-383849 Share on other sites More sharing options...
otuatail Posted November 2, 2007 Share Posted November 2, 2007 Are you trying to select records from a table that are between before or after? if you are using date fields then they will both be 'YYYY-MM-dd' Desmond Quote Link to comment https://forums.phpfreaks.com/topic/75840-determine-if-date-is-in-past-or-present-future/#findComment-383864 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.