Jump to content

Retrieve record by date please help


geroid

Recommended Posts

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"); 

Link to comment
https://forums.phpfreaks.com/topic/169219-retrieve-record-by-date-please-help/
Share on other sites

  Quote

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

$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.

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.