简体   繁体   中英

PHP Time if extends

for now this is what my code contains

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";
}
?>

My problem is, even if I put for exampole 08:00 AM on that day, it still says You are past the curfew time. Any thoughts?

The date() function formats a timestamp to a string, when you compare the two strings using $formattedTime > $curfew PHP converts the two strings to integers, which would make something like 8:41 AM simply 8 .

Here's a quick sloppy hack that should work correctly

<?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";
}
?>

Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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