简体   繁体   English

MySQL和PHP的日期范围

[英]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. 这是我的PHP代码。 I tried using the BETWEEN and the <= to grab the date range but it does not seem to work. 我尝试使用BETWEEN和<=来获取日期范围,但它似乎不起作用。 When I printed the results to the console, I just get a Uncaught SyntaxError: Unexpected number. 当我将结果打印到控制台时,我只是得到一个Uncaught SyntaxError:意外的数字。 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>";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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