douyi1855 2014-06-20 18:29
浏览 59
已采纳

MySQL和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>";
    }
  • 写回答

1条回答 默认 最新

  • duandian2725 2014-06-20 18:37
    关注

    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>";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题