PHP 和 MySQL 的日期范围

标签: MySQL PHP
发布时间: 2017/4/9 23:12:26
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我似乎有问题,尝试获取某个特定日期范围内的表时。这里是我的 php 代码。我尝试过使用 BETWEEN 和 < = 抢日期范围但它似乎并不工作。当我将结果打印到控制台时,我只是得到未捕获的 SyntaxError︰ 意外的数目。有何建议?

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

解决方法 1:

这是很难诊断没有看到其余的脚本,但似乎你字符串串联可能是一个因素像这行上︰

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

只需添加一个空格开头的像这样︰

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

另外,你似乎会丢失在字符串的末尾那里一个单引号。所以就像这样︰

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

当您使用的双引号,你可以做字符串替换,所以你不需要连接这些线与 . 的每个字符串的出现。

这就是说,在这里我清理您的东西上面我提到的解决每个实例的代码。

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>";
    }
官方微信
官方QQ群
31647020