日期输入字段在php中应该是可选的

时间:2018-08-20 09:55:10

标签: php mysql

大家好,我有一个用户将从下拉菜单中选择课程,而第二个输入用户将选择应该是可选的过期日期。因此,当用户未提供日期时,它将显示为{{1} }

如果我也没有在输入字段中输入任何日期,我不知道为什么会这样显示。

这是我的代码:

Your Due date should be in future.

这是我的表格:

if (isset($_POST['assigncourse'])) {

    $courseid = $_POST['courseid'];
    //$duedate = date('Y-m-d', strtotime($_POST['duedate']));
    $duedate = strtotime($_POST['duedate']);
    $now = strtotime(date('Y-m-d'));
    if ($now < $duedate) 
    {
        $courseid = required_param('courseid', PARAM_TEXT);
        $groupid = required_param('groupid', PARAM_TEXT);

        $insert_record = new stdClass();
        $insert_record->courseid = $courseid;
        $insert_record->groupid = $_SESSION['groupid'];
        $insert_record->duedate = $duedate;
        $insert_record->createdby= $id;
        $insert_record->createdon = date('Y-m-d H:i:s');
        $sql1 = "SELECT *  
                    FROM mdl_ppc_assigncourses_group 
                    WHERE groupid='".$_SESSION['groupid']."' 
                    AND courseid='$courseid'";
        $courses = $DB->get_records_sql($sql1);
        if (count($courses) > 1) {
            $errorMessage = "The course already taken!";
        } else {
        $DB->insert_record('ppc_assigncourses_group', $insert_record);
        }
    } else {
        $errorMessage = "Your Due date should be in future.";
    }
}

有人可以帮我犯什么错误吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

问题是未设置Duedate。 $ now不能小于截止日期。

此行:

if ($now < $duedate)

您将不得不进行更改。使其看起来是否$ duedate设置在第一位。

    if (isset($_POST['assigncourse'])) {

    $courseid = $_POST['courseid'];
    //$duedate = date('Y-m-d', strtotime($_POST['duedate']));


    if(isset($_POST['duedate']) && ($_POST['duedate'] != "")) {
          $duedate = strtotime($_POST['duedate']);
        }
        else {
          $duedate = false;
        }
        $now = strtotime(date('Y-m-d'));
        if ((!$duedate) || ($now < $duedate)) 
        {
            $courseid = required_param('courseid', PARAM_TEXT);
        $groupid = required_param('groupid', PARAM_TEXT);

        $insert_record = new stdClass();
        $insert_record->courseid = $courseid;
        $insert_record->groupid = $_SESSION['groupid'];
        if(is_numeric($duedate)) {
          $insert_record->duedate = $duedate;
        }
        else {
           $insert_record->duedate = time();
        }
        $insert_record->createdby= $id;
        $insert_record->createdon = date('Y-m-d H:i:s');
        $sql1 = "SELECT *  
                    FROM mdl_ppc_assigncourses_group 
                    WHERE groupid='".$_SESSION['groupid']."' 
                    AND courseid='$courseid'";
        $courses = $DB->get_records_sql($sql1);
        if (count($courses) > 1) {
            $errorMessage = "The course already taken!";
        } else {
        $DB->insert_record('ppc_assigncourses_group', $insert_record);
        }
    } else {
        $errorMessage = "Your Due date should be in future.";
    }
}

ViewPage:

<td><?php if($duedate > 0) { echo date('Y-M-d',$duedate); } ?></td>

答案 1 :(得分:1)

您需要检查$duedate是否具有值。您会这​​样做的。

if(isset($duedate) && $duedate){

if(strtotime($now) < strtotime($duedate)){

echo 'Do something';

}else{

echo 'Do something else.';

}

}else{

echo 'You need to enter a due date.';

}

您的代码如下:

if (isset($_POST['assigncourse'])) {

    $courseid = $_POST['courseid'];
    //$duedate = date('Y-m-d', strtotime($_POST['duedate']));
    //$duedate = strtotime($_POST['duedate']);
    $now = date('Y-m-d');

    if(isset($_POST['duedate']) && $_POST['duedate']){


      if(strtotime($now) < strtotime($_POST['duedate'])) 
      {
          $courseid = required_param('courseid', PARAM_TEXT);
          $groupid = required_param('groupid', PARAM_TEXT);

          $insert_record = new stdClass();
          $insert_record->courseid = $courseid;
          $insert_record->groupid = $_SESSION['groupid'];
          $insert_record->duedate = $duedate;
          $insert_record->createdby= $id;
          $insert_record->createdon = date('Y-m-d H:i:s');
          $sql1 = "SELECT *  
                      FROM mdl_ppc_assigncourses_group 
                      WHERE groupid='".$_SESSION['groupid']."' 
                      AND courseid='$courseid'";
          $courses = $DB->get_records_sql($sql1);
          if (count($courses) > 1) {
              $errorMessage = "The course already taken!";
          }else{
          $DB->insert_record('ppc_assigncourses_group', $insert_record);
          }
      }else{
          $errorMessage = "Your Due date should be in future.";
      }
    }

}