Skip to content Skip to sidebar Skip to footer

Php Date Validation Function

I have a simple form on my website that asks for an event ID, a date and an amount. I would like to make sure that whatever date the user enters is greater than 10 days in the fut

Solution 1:

With HTML5, there's a min (and max) attribute for date-inputs, which takes YYYY-MM-DD formats as arguments.

<inputtype="date"min="2017-09-01" name="my_date" />

You can then use PHP to generate the day that's 10 days into the future, and set that date via PHP.

<?php$date = new DateTime("+10 days");
?><inputtype="date"min="<?=$date->format("Y-m-d"); ?>"name="my_date" />

That being said, this will only be client-side. It'll not allow the user to select any days between today and the next 10 days in the date-selector. However, its possible to circumvent this - simply because anything on the client-side can be manipulated. Which is why you should always do server-side validation on whats coming from the client. Using DateTime objects, you can just compare them directly, as shown below.

<?php$date_input = new DateTime($_POST['my_date']); 
$date_limit = new DateTime("+10 days");
if ($date_input > $date_limit) {
    // Valid date! It's not in the next 10 days
}

Solution 2:

Check against UNIX time + 10 days in seconds.

$date = "2017-09-05";

If(strtotime($date) > time()+(60*60*24*10)){
    Echo "ok";
}

Post a Comment for "Php Date Validation Function"