geroid Posted August 7, 2009 Share Posted August 7, 2009 Hi Can anyone help me with this problem. I have records in a database and I want to display them by a date condition. I want a record to be displayed if the date is later than yesterdays date. That is to say only records from todays date onwards should be retrieved and the older ones left behind. Records should not be displayed that are older than todays date or future dates. Any ideas what to add as a where clause to my code? $query = "select event_num, event_name, venue_name, streetaddress1, town, county, event_description_ire, event_description_eng, date_format(event_date, '%M %D, %Y ') as formatteddate, event_time from $table order by event_num desc"; $results = mysql_query($query, $link) or die("Sorry, We could not connect to the database"); Quote Link to comment https://forums.phpfreaks.com/topic/169219-retrieve-record-by-date-please-help/ Share on other sites More sharing options...
watsmyname Posted August 7, 2009 Share Posted August 7, 2009 Hi Can anyone help me with this problem. I have records in a database and I want to display them by a date condition. I want a record to be displayed if the date is later than yesterdays date. That is to say only records from todays date onwards should be retrieved and the older ones left behind. Records should not be displayed that are older than todays date or future dates. Any ideas what to add as a where clause to my code? $query = "select event_num, event_name, venue_name, streetaddress1, town, county, event_description_ire, event_description_eng, date_format(event_date, '%M %D, %Y ') as formatteddate, event_time from $table order by event_num desc"; $results = mysql_query($query, $link) or die("Sorry, We could not connect to the database"); Do something like this <?php $yesterday= mktime(0, 0, 0, date("m"), date("d")-1, date("y")); $yesterday= date("Y-m-d",$yesterday); $query = "select event_num, event_name, venue_name, streetaddress1, town, county, event_description_ire, event_description_eng, date_format(event_date, '%M %D, %Y ') as formatteddate, event_time from $table WHERE DATE_FORMAT(event_date,'%Y-%m-%d')>$yesterday order by event_num desc"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/169219-retrieve-record-by-date-please-help/#findComment-892882 Share on other sites More sharing options...
abazoskib Posted August 7, 2009 Share Posted August 7, 2009 $query = "select event_num, event_name, venue_name, streetaddress1, town, county, event_description_ire, event_description_eng, date_format(event_date, '%M %D, %Y ') as formatteddate, event_time from $table WHERE DAYOFYEAR(event_date) > DAYOFYEAR(DATE_SUB(NOW(), INTERVAL 2 DAY)) order by event_num desc"; $results = mysql_query($query, $link) or die("Sorry, We could not connect to the database"); That should work for most of the year. I think it might failt on January 1st, so best way to combat that is check for dayofyear, and year. Quote Link to comment https://forums.phpfreaks.com/topic/169219-retrieve-record-by-date-please-help/#findComment-892886 Share on other sites More sharing options...
watsmyname Posted August 7, 2009 Share Posted August 7, 2009 I missed ")" in my query, i have edited my post check it Quote Link to comment https://forums.phpfreaks.com/topic/169219-retrieve-record-by-date-please-help/#findComment-892889 Share on other sites More sharing options...
geroid Posted August 7, 2009 Author Share Posted August 7, 2009 Thanks for the replies. They have given me some ideas. I'll experiment with that. Quote Link to comment https://forums.phpfreaks.com/topic/169219-retrieve-record-by-date-please-help/#findComment-893240 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.