繁体   English   中英

PHP时间如果延长

[英]PHP Time if extends

现在这是我的代码包含的内容

index.html

<form action="timeout.php" method="POST">
<input type="time" name="txt_time" required>
<input type="submit">
</form>

timeout.php

<?php
$formattedTime = date("h:i A", strtotime($_POST['txt_time']));
$curfew = date("h:i A", strtotime('20:00'));
if($formattedTime > $curfew) {
 echo "You are past the curfew time";
} else {
 echo "Success";
}
?>

我的问题是,即使我当天参加上午08:00考试,它仍然说您已经过了宵禁时间。 有什么想法吗?

当您使用$formattedTime > $curfew比较两个字符串时, date()函数将时间戳格式化为字符串,PHP将两个字符串转换为整数,这将使8:41 AM变成8

这是一个快速的草率黑客,应该可以正常使用

<?php
$formattedTime = intval(date("Hi", strtotime($_POST['txt_time'])));
$curfew = intval(date("Hi", strtotime('20:00')));
//'H' will get the hour of the time from 0-23, intval will convert that to an integer

if($formattedTime > $curfew) {
    echo "You are past the curfew time";
} else {
    echo "Success";
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM