Jump to content

Filter records between 2 dates


Recommended Posts

Hi everyone, I have a list containing all records in a table. Upon inserting these records a the date is also inserted into the record. I need to create a filter where I can pick 2 dates and filter the table showing only those records added within the 2 dates.

Ive no idea how or how to start working on this so could anybody provide me ideas?

Link to comment
https://forums.phpfreaks.com/topic/202271-filter-records-between-2-dates/
Share on other sites

As it's unlikely you are using a PDO interface (since you don't have a clue what Daniel is talking about), your code would look something more like...

 

mysql_query("SELECT * FROM worksorders WHERE worksorders.intradateadd BETWEEN '{$_POST['date1']}' AND '{$_POST['date2']}'");

Though as Daniel said you would want to validate/sanitize the $_POST variables first.

Okay I now have my dates worked, I just need to put them into my query, my date variables are

<?php 
$startdate = date('Y-m-d', strtotime ("last Monday"));  
$enddate = date('Y-m-d', strtotime ("next Friday"));

echo $startdate;
echo $enddate;
?>

 

and my query is

SELECT worksorders.intrajobno, worksorders.intratype, worksorders.intrastatus, worksorders.intracustomer, worksorders.intradescription, worksorders.intraorder, worksorders.intraartapproval, worksorders.intraestimatedinstall, worksorders.intraprio, worksorders.intrajobvalue, worksorders.intraterms, worksorders.intrajobref, worksorders.id, worksorders.intradelete FROM worksorders WHERE {$NXTFilter_rsworksorders1} AND worksorders.intradelete != 0 ORDER BY {$NXTSort_rsworksorders1}";

 

Ive tried...

SELECT worksorders.intrajobno, worksorders.intratype, worksorders.intrastatus, worksorders.intracustomer, worksorders.intradescription, worksorders.intraorder, worksorders.intraartapproval, worksorders.intraestimatedinstall, worksorders.intraprio, worksorders.intrajobvalue, worksorders.intraterms, worksorders.intrajobref, worksorders.id, worksorders.intradelete, worksorders.intradateadd FROM worksorders WHERE {$NXTFilter_rsworksorders1} AND worksorders.intradelete != 1 AND worksorders.intradateadd BETWEEN '{$startdate}' AND '{$enddate'); ORDER BY {$NXTSort_rsworksorders1}"

To no avail, Can anybody see why? are my variables in the query incorrect?

Ive also just tried

$query_rsworksorders1 = "SELECT worksorders.intrajobno, worksorders.intratype, worksorders.intrastatus, worksorders.intracustomer, worksorders.intradescription, worksorders.intraorder, worksorders.intraartapproval, worksorders.intraestimatedinstall, worksorders.intraprio, worksorders.intrajobvalue, worksorders.intraterms, worksorders.intrajobref, worksorders.id, worksorders.intradelete FROM worksorders WHERE {$NXTFilter_rsworksorders1} AND worksorders.intradelete != 1 AND worksorders.intradateadd BETWEEN "$startdate" AND "$enddate" ORDER BY {$NXTSort_rsworksorders1}";

I read variables in queries need double quotes, I tried this, again without success

Even a basic syntax highlighting software package should point out the various issues with that. Variables will only be evaluated when they are contained within a string that is delimited by double quotes this doesn't mean the variables themselves should be delimited by them. Any value which is string based, which is being passed to MySQL need to be delimited by quotes (single or double, but single makes most sense because the string object in PHP is itself delimited by double quotes, so if you use double quotes you'll have to escape them). When placing variables in strings I find it useful to delimit them with curly braces as it helps to differentiate the difference between the variable name and part of the string eg

 

$var = "like;
echo "{$var}able";
echo "$varable"; 

Sorry cags im new to php and now im completely lost, by the value needs to be delimited do you mean..

<?php 
$startdate = 'date('Y-m-d', strtotime ("last Monday"));' 
$enddate = 'date('Y-m-d', strtotime ("next Friday"));'
?>

 

I know this is wrong as the highlighting suggests so but im completely dumbfounded

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.