Question

SO I have write the code for getting the list of upcoming events in next 7 days and it is working fine and giving me the list of upcoming events.but the probmlem is that it is also giving the event record of past days also. Here is my code :

$today = strtotime("Now"); 
echo date('Y-m-d :H:i:s',$today)."<br>"; 
$leadDate = strtotime("+7 day", $today);
echo date('Y-m-d :H:i:s',$leadDate)."<br>"; 
$rs = mysql_query("SELECT * FROM names WHERE due_date BETWEEN CURDATE() AND '$leadDate'") or die(mysql_error());
echo "<b>Dues for upcoming 7 days</b><br>";
while($row=mysql_fetch_assoc($rs))
{
    echo $row['name']." => ".$row['normal_date']."<br>";
} 

I have the table :

id| name | due_date | normal_date
|1|name1|1390245240|Mon, 20 Jan 2014 19:14:00 GMT
|2|name2|1390331640|Tue, 21 Jan 2014 19:14:00 GMT
|4|name3|1390418040|Wed, 22 Jan 2014 19:14:00 GMT
|5|name4|1390590840|Fri, 24 Jan 2014 19:14:00 GMT
|6|name5|1390850040|Mon, 27 Jan 2014 19:14:00 GMT
|7|name6|1390936440|Tue, 28 Jan 2014 19:14:00 GMT
|8|name7|1396034040|Fri, 28 Mar 2014 19:14:00 GMT
|9|name8|1398708840|Mon, 28 Apr 2014 18:14:00 GMT
|10|name9|1390158840|Sunday, 19 Jan, 2014
|11|name10|1390072440|Saturday, 18 Jan, 2014
|12|name11|1390176000|20 Jan, 2015

server's today's date is 19 Jan 2014 but it is also giving me the list of 18 Jan 2014 but i am looking for the next 7 days record.and my current script is giving me the following out put:

2014-01-19 :20:44:30
2014-01-26 :20:44:30
Dues for upcoming 7 days
name1 => Mon, 20 Jan 2014 19:14:00 GMT
name2 => Tue, 21 Jan 2014 19:14:00 GMT
name3 => Wed, 22 Jan 2014 19:14:00 GMT
name4 => Fri, 24 Jan 2014 19:14:00 GMT
name9 => Sunday, 19 Jan, 2014
name10 => Saturday, 18 Jan, 2014
name11 => 20 Jan, 2015

So Why the 18 jan 2014 & 20 jan 2014 coming in record, where i am wrong.Please Help. Thanks in Advance.

Was it helpful?

Solution

Try this code:

$rs = mysql_query("SELECT * FROM names WHERE due_date BETWEEN '$today' AND '$leadDate'") or die(mysql_error());

Instead of the CURDATE function use the $today.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top