2

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 future. I dont want my clients making payments online if their event is less than 10 days out. Once they enter the info, they are directed to a secure website where they can make the payment which was provided by my bank.

Is there a way to check the date on submit or via some kind of javascript that uses the modified object property that would alert the user that the date is invalid?

Any help would be amazing. Thanks! Rich

1
  • 1
    Of course the answer is "yes", there is always a way. But asking people here to write code for you isn't really what SO is all about. Please try something yourself and post code when you get stuck. Commented Aug 22, 2017 at 15:40

2 Answers 2

2

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

<input type="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");
?>
<input type="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
}
Sign up to request clarification or add additional context in comments.

3 Comments

Great solution! Uses Datetime object so it won't break in the near future!
@ctwheels I was writing an answer of this kind :(
Thanks everyone! I will give it a try
0

Check against UNIX time + 10 days in seconds.

$date = "2017-09-05";

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

14 Comments

@ctwheels what do you want me to do? Change the php interpreter to 64bit?
Use Datetime instead?
@ctwheels I'm sure this will not be a problem when this problem is nearing. When strtotime becomes 64bit this code will work. However if someone pays for an event now 20 years ahead. Yes it will be a problem. But I'd say it's not likely.
$date = new Datetime('2017-09-05') and if($date > new Datetime('+10 days')) - Much more legible, not reliant on 64-bit, works if someone creates an event commemorating Y2038 problem. Why give people code that will eventually break instead of giving them proper code to begin with? That's like saying "Ya someone might do some SQL injection to my code eventually, but I'll fix it when the time comes" - no, do it right the first time.
If you really want to continue with non-updated codes and use older functions go ahead. There's nothing wrong with that is it does work (as I previously stated). The Datetime object, however, does follow OOP and does generally provide a better solution to handling dates in PHP. There's a reason .NET uses DateTime structure.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.