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

Rich Gags
  • 21
  • 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. – But those new buttons though.. Aug 22 '17 at 15:40

2 Answers2

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
}
Qirel
  • 25,449
  • 7
  • 45
  • 62
0

Check against UNIX time + 10 days in seconds.

$date = "2017-09-05";

If(strtotime($date) > time()+(60*60*24*10)){
    Echo "ok";
}
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • @ctwheels what do you want me to do? Change the php interpreter to 64bit? – Andreas Aug 22 '17 at 15:54
  • Use `Datetime` instead? – ctwheels Aug 22 '17 at 15:54
  • @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. – Andreas Aug 22 '17 at 15:58
  • `$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. – ctwheels Aug 22 '17 at 16:01
  • @ctwheels it's 20 years!!! How many webpages do you know that not only exsisted but has kept the same code for 20 years? Strtotime is not wrong and I'm sure when php becomes 64bit strtotime will also work with later years. And note that 20 years is pretty close to how old php is. Did you go around telling people 20 years ago to not use MD5 or MySQL? If you really did then you should not recommend datetime today since we don't know if that is "the best" in 20 years. – Andreas Aug 22 '17 at 16:10
  • I completely agree, but I know a lot of projects that don't change in 10-15 and even more years. May as well provide an answer that will work even if it's no longer maintained. [Space Jam](http://www2.warnerbros.com/spacejam/movie/jam.htm) has remained unchanged since 1996 by the way. I know things will change in the languages and improvements will be made and yes, one day datetime object may not be used and we might say "use datetime2 object instead" but until that day comes, we should be applying best practices. You wouldn't tell people to use MD5 or mysql_* extensions today, why strtotime? – ctwheels Aug 22 '17 at 16:18
  • Don't get me wrong, your solution works and yes, I'd accept it as a potential answer. I just believe there are better answers and (at the moment) that better answer is to use the `Datetime` object – ctwheels Aug 22 '17 at 16:22
  • You are liking to a page about a film from 96 and saying it has not been changed? How do you know? Seriously, link to a real page if you want to make a point. In my opinion if you would answer with unsupported functions then yes I agree that it would be bad answer. But since strtotime and time() are two very common functions used by many it's not wrong. And since datetime has a higher overhead and is slower why bother with it? It's still too far ahead in the future to care about. "The millennium bug" wasn't fixed until 1999 or somewhere close to it. – Andreas Aug 22 '17 at 16:30
  • [The Space Jam website hasn't changed since 1996 and we're getting all nostalgic](http://metro.co.uk/2016/05/22/the-space-jam-website-hasnt-changed-since-1996-and-were-getting-all-nostalgic-5897807/). It's a pretty real page. It was made for the movie Space Jam? I don't know what a "real page" means if that's not considered real? Btw, overhead, really? [Here's an answer that talks about the Datetime overhead](https://stackoverflow.com/a/8605650). Sometimes overhead is worth it: [Manageability, Maintainability, and Supportability](https://msdn.microsoft.com/en-us/library/bb896744.aspx) – ctwheels Aug 22 '17 at 16:38
  • The page you link to now is nothing like the page you liked to earlier. It says: `This web site is currently not available. Please check us out at these other Warner Bros. web sites:` at the first link. Still datetime is slower https://en.code-bude.net/2013/12/19/benchmark-strtotime-vs-datetime-vs-gettimestamp-in-php/ – Andreas Aug 22 '17 at 17:05
  • This is the page you linked to first titled "Space Jam" http://forumbilder.se/G99RB/screenshot-2017-08-22-19-08-07 – Andreas Aug 22 '17 at 17:09
  • My apologies, here's the correct link: https://www.warnerbros.com/archive/spacejam/movie/jam.htm – ctwheels Aug 22 '17 at 17:13
  • And if the extension of that file is what we think it is, then it's a completely static page. You can make a htm(l) page dynamic but I'm going guess this is not such a case. With static pages it's a completely different thing. Since I'm on my phone, I can't (or not interested) see if the page has used the deprecated font tag. If they have then it's a clear case of a page 20 years old with deprecated (or old style) functions still working. And that is proving my point in two points. 1 unnecessary to care about the future. 2 tags/functions that is "it" today is not the hot stuff in 20 years – Andreas Aug 22 '17 at 17:30
  • 1
    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](https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx). – ctwheels Aug 22 '17 at 17:36