-4

I need a function to validate any string provided in GET['date'] parameter.

The function will return true if the string provided is in format "j-n-Y", and is a date between today and the future.

Examples:

http://mysite.org/index.php?date=iwhechiuhwe will return false.

http://mysite.org/index.php?date=13-5-2017 will return true.

http://mysite.org/index.php?date=13-5-2010 will return false.

http://mysite.org/index.php?date=19-5-2014 will return true.

Juan Del Árbol
  • 138
  • 3
  • 13

1 Answers1

0

$dateResult = DateTime::createFromFormat('j-n-Y', $_GET['date']);

This method in PHP's DateTime class returns false if it couldn't create a date object from the requested format and given string.

A further test to verify that the given date was today or future (if $dateResult is not already false):

$now = strtotime('Y-m-d', time());
$isValid = $dateResult->getTimestamp() >= $now;
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
  • ¡Thank you! It works :) The users that have given downvote and marked this as too abroad, ¿really? ¿Is that so difficult to answer? – Juan Del Árbol May 19 '14 at 14:38
  • I don't think too broad is a valid close reason. It's a question that has probably come up before, which will be why it was downvoted. But the linked question in the comments above didn't have a good solution. – bcmcfc May 19 '14 at 14:42