-1

im trying to validate a date in the YYYY-MM-DD in the $immerseusnorm i have tried the code below but its returning No!

<?php
$json = file_get_contents("http://www.wowtrack.org/plugins/guild/EU/Emerald%20Dream/VII?format=json", true);
$decode = json_decode($json, true);
$realmrank = " ". $decode[realmRank] ."";
$immerseusnorm = " ". $decode[encounters][0][completedOn]."";

if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$immerseusnorm))
{
$immerseusnorm = "Yes";
}
$immerseusnorm = "No";

echo "Realm Rank: $realmrank<br \>";
echo "Immerseus Normal - Completed On: $immerseusnorm<br \>"

Any help would be great

Thanks

ids
  • 35
  • 1
  • 10

3 Answers3

2

I would attempt to create a DateTime object based on the format. That way, you can validate both the format AND that the date it suggests is valid. For example, a simple regex match with your pattern would allow something like 2014-02-31, which obviously is not a valid date.

The only challenge with DateTime is that a date like 2014-02-31 gets "fixed" to 2014-03-03, instead of returning false from DateTime::createFromFormat(). So you can just check back against the input string to make sure the date didn't get changed.

Putting it into a function could look like this:

function validate_date($input_date, $format = 'Y-m-d') {
    $datetime = DateTime::createFromFormat($format, $input_date);
    if(false === $datetime) {
        return false;
    } else if ($datetime->format($format) === $input_date) {
        return true;
    } else {
        return false;
    }
}


// usage
if (validate_date($immerseusnorm)) {
    // validation passed
} else {
    // validation failed
}
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • I would not recommend using identical comparison operator in the compare format part, because for an example, user can enter integer number like `validateDate(2014, 'Y')`; in this case `false` will be returned. **[And this function can be simplified to 2 lines, like this.](http://stackoverflow.com/a/12323025/67332)** – Glavić Feb 28 '14 at 23:48
  • @Glavić I would absolutely use an identical operator. In fact, if I was using this function in production I would force string input and I uld throw an Exception if the input was not a string input. Note that `createFromFormat()` method expects string as input for date. Yes, the function can be collapsed down, but there are no bonus points awards for the shortest possible code. For demonstration purposes, I want to make sure the example is clearly understandable. – Mike Brant Feb 28 '14 at 23:57
1

I guess you should add else around the second $immerseusnorm, it now always sets it to no. So better change this:

if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$immerseusnorm))
{
$immerseusnorm = "Yes";
}
$immerseusnorm = "No";

to:

if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$immerseusnorm))
{
    $immerseusnorm = "Yes";
}else{
    $immerseusnorm = "No";
}
hugo jan
  • 176
  • 5
  • You might want to point out what the difference is and why it is different – John Conde Feb 28 '14 at 20:41
  • You're right, I have added some introduction. Guessed it would be clear from the indentation. – hugo jan Feb 28 '14 at 20:43
  • for some reason after using `$immerseusnorm = " ". $decode[encounters][0][completedOn].""; if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$immerseusnorm)) { $immerseusnorm = "Yes"; }else{ $immerseusnorm = "No"; }` i am still getting no if i use it without the code i get `2013-09-11` – ids Feb 28 '14 at 22:45
0

It seems that your regex is working.
you have missed a point here. this line is always executed :

$immerseusnorm = "No";

You should have else of your if condition.

try this:

if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$immerseusnorm))
{
$immerseusnorm = "Yes";
}
else{
$immerseusnorm = "No";
}

Sample demo : see demo : https://eval.in/107333

Awlad Liton
  • 9,366
  • 2
  • 27
  • 53