Limit User Input of HTML <input type = "time"> After Current Time

Viewed 704

As per the title, I'm trying to think of a way to limit the user input of a field after the current time. Say if it is 10:00 AM now, the field would only allow input of the time after 10:00 AM, e.g. 11:00 AM, 12:00 PM etc.

I have tried the following but to no avail:

<?php $currenttime = time(); <input type="time" name="time" min="<?php echo $currenttime?>">

I believe this is due to the PHP time() function outputting it as a Unix timestamp, rather than in hours, minutes and seconds. Is there any solution to this? Preferably using PHP or Javascript?

2 Answers

Use date to output current time in desired format. If it's hours, minutes and seconds you seek, then it'd be date('H:i:s').

You can use date to format timestamps. If no timestamp is provided to date, it defaults to the current timestamp.

<?php $currenttime = date("H:i"); ?>
<input type="time" name="time" min="<?php echo $currenttime?>">
Related