表格验证开启和关闭时间PHP

时间:2017-06-11 11:23:18

标签: php forms validation time

我想知道如何设置用户在9:00到20:00范围内输入首选开始时间和结束时间,但我认为我的代码的问题是变量类型不正确。

这是表单输入:

<div class="form-group">
       <label>Start Time:<br></label>
       <input type="time" name="starttime" class="form-control" value="<?php echo $_POST['starttime']?>" required>
     </div>
     <div class="form-group">
       <label>End Time:<br></label>
       <input type="time" name="endtime" class="form-control" value="<?php echo $_POST['endtime']?>" required>
     </div>

以及将处理数据的PHP代码:

if(isset($_POST['submit'])){
            echo $open = "9:00";   #declaration for the opening time
            echo " - ";
            echo $close = "20:00";  #declaration for the closing time
            echo "<br>";
            echo $starttime = $_POST['starttime']." - ";
            echo $endtime = $_POST['endtime'];


            echo "<br>time elapsed: ".$total_time = $endtime - $starttime;

            if($starttime>$open){
              echo "Restaurant is open at these hours";
            }else{

            #this part validates the time range after the user's preferred time is within the operating hours of the resto
                      if($total_time > 4){
                      echo "<script>alert('Time Range is too large. Max stay is 4 hours')</script>";
                    } elseif($total_time<1) {
                      echo "<script>alert('Time Range is small.')</script>";
                    }else{
                      echo "oks";
                    }
              echo "string";
            }


          }

错误是它没有转到else语句,我认为这是因为解析变量和比较问题。有没有什么方法可以转换这两个变量以创建大于或小于比较?

1 个答案:

答案 0 :(得分:0)

我会尝试在比较之前将字符串变量转换为浮点数。个人的事情,但我不相信PHP比较字符串中更大的数字。

我会改变

如果($开始时间&GT; $开放){

要 像:starttime =(float)$ startime; $ open相同。 如果($开始时间&GT; = $打开){

这个答案应该只是一个评论,但我还没有足够的启动。对不起。