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

Of course...

 

$stmt = $db->prepare('SELECT something FROM stuff WHERE created_at BETWEEN ? AND ?');
$stmt->execute(array($_POST['start'], $_POST['end']));

$stuff = $stmt->fetchAll();

 

(Assuming a PDO interface and that the values have already been validated)

Link to comment
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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.