简体   繁体   中英

Date range with MySQL and PHP

I seem to be having an issue when trying to get tables within a specific date range. Here is my php code. I tried using the BETWEEN and the <= to grab the date range but it does not seem to work. When I printed the results to the console, I just get a Uncaught SyntaxError: Unexpected number. Any suggestions?

if($_POST['subdate'] != "")
    {

        $sdate = dateClean($_POST['subdate']);
        $edate = dateClean($_POST['subdateend']);
        if($checkWhereVar==0)
        {

            if($_POST['subdateend'] != "")
            {

                $longQuery.= "SubmissionDate >= '".$sdate."' AND SubmissionDate <= '".$edate."";
                echo ("<script>console.log('$longQuery')</script>");
            }
            else{

                $date = date('Y/m/d H:i:s');
                $edate = $date;
                $longQuery.= "SubmissionDate >= '".$sdate."' AND SubmissionDate <= '".$edate."";

            }

            //$longQuery.= " SubmissionDate BETWEEN = '".$sdate."'";
            $checkWhereVar = 1;
        }
        else
        {
             $date = date('Y/m/d H:i:s');
             $edate = $date;
            echo ("<script>console.log('in sub date else')</script>");
            $longQuery.= "SubmissionDate >= '".$sdate."' AND SubmissionDate <= '".$edate."";
        }
        $filter .= " Submission Date: <i>".$_POST['subdate']."</i>";
    }

This is hard to diagnose without seeing the rest of the script, but it seems that your string concatenation might be a contributing factor on lines like this:

$longQuery.= "SubmissionDate >= '".$sdate."' AND SubmissionDate <= '".$edate."";

Just add a single space at the beginning of that like this:

$longQuery.= " SubmissionDate >= '".$sdate."' AND SubmissionDate <= '".$edate."";

Also, you seem to be missing a single quote there at the end of the string. So just do this:

$longQuery.= " SubmissionDate >= '$sdate' AND SubmissionDate <= '$edate'";

When you use double quotes you can do string substitution so you don't have to concatenate those lines with . where every string shows up.

That said, here is my cleanup of your code with each instance of the stuff I mentioned above addressed.

if($_POST['subdate'] != "")
    {

        $sdate = dateClean($_POST['subdate']);
        $edate = dateClean($_POST['subdateend']);
        if($checkWhereVar==0)
        {

            if($_POST['subdateend'] != "")
            {

                $longQuery.= " SubmissionDate >= '$sdate' AND SubmissionDate <= '$edate'";
                echo ("<script>console.log('$longQuery')</script>");
            }
            else{

                $date = date('Y/m/d H:i:s');
                $edate = $date;
                $longQuery.= " SubmissionDate >= '$sdate' AND SubmissionDate <= '$edate'";

            }

            //$longQuery.= " SubmissionDate BETWEEN = '$sdate'";
            $checkWhereVar = 1;
        }
        else
        {
             $date = date('Y/m/d H:i:s');
             $edate = $date;
            echo ("<script>console.log('in sub date else')</script>");
            $longQuery.= " SubmissionDate >= '$sdate' AND SubmissionDate <= '$edate'";
        }
        $filter .= " Submission Date: <i>".$_POST['subdate']."</i>";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM