Filtering Records using Date range!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pavanesh2009
Forum Commoner
Posts: 30
Joined: Wed Jan 13, 2010 7:24 am

Filtering Records using Date range!!

Post by pavanesh2009 »

Hi ALL,

I am working on a application wher i need to filter some records using a particular date range, I have used jquery datepicker & able to get two dates (i.e. FROM: To:) now after getting this date value i need to fetch data from multiple tables, so i used joins but how i should i use Between clause for getting a date range.Here is my code:


<?php
$start_date=date("Y-m-d",strtotime($_POST['obdate']));
$end_date=date("Y-m-d",strtotime($_POST['obdate1']));

/* these are two dates in between i need to filter my records */

$sql =mysql_query("SELECT user_tree_table.user_tree_id,tree_nickname,
user_tree_observations.observation_id,user_tree_observations.date,
Species_master.species_primary_common_name
FROM user_tree_observations
INNER JOIN
(users, user_tree_table, trees,Species_master)
ON user_tree_table.user_tree_id = user_tree_observations.user_tree_id
AND user_tree_table.user_id='$_SESSION[user_id]'
AND trees.species_id=Species_master.species_id
/* stuff for date range----
AND user_tree_observations.date=(BETWEEN '$start_date' AND '$end_date')
*/
ORDER BY user_tree_table.tree_id");

while($row = mysql_fetch_array($sql))
{
print $row['user_tree_id'];
print "<td>".$row['date']."</td>";
print "<td>".$row['tree_nickname']."</td>";
}
?>

Tried to use BETWEEN but seems not to be working ,Please let me know how i can solve this problem,
Thanks In Advance!!
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Filtering Records using Date range!!

Post by manohoo »

try this:

Code: Select all

AND user_tree_observations.date BETWEEN '$start_date' AND '$end_date'
Notice that I am not using "="
pavanesh2009
Forum Commoner
Posts: 30
Joined: Wed Jan 13, 2010 7:24 am

Re: Filtering Records using Date range!!

Post by pavanesh2009 »

Absolutely correct, I got this idea yesterday itself after breaking my head for three hours, sorry could not update forum post.

Well, Thanks a lot manohoo for giving your time on this.Now it works like a charm.. :D


manohoo wrote:try this:

Code: Select all

AND user_tree_observations.date BETWEEN '$start_date' AND '$end_date'
Notice that I am not using "="
Post Reply