【发布时间】:2017-06-11 11:23:18
【问题描述】:
我想知道如何设置用户在 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语句,我认为是因为解析变量和比较的问题。有什么方法可以转换两个变量以创建大于或小于比较?
【问题讨论】:
-
您将 echo 与变量设置混合在一起,不建议这样做
标签: php forms validation time