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
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.