-3

i have a string in this format. mm/dd/yyyy.i want to check this string is a valid date or not in php without using regex.anyone to help me..?

Jasmel Pc
  • 515
  • 1
  • 5
  • 16
  • 3
    *anyone to help me..?* If you need help, because you got stuck somewhere yes. Free code, no. So show your current code – Rizier123 Sep 02 '15 at 15:53
  • Why no regex? Not using regex for this makes things unneccessary complicated – Philipp Sep 02 '15 at 15:56
  • I'd recommend reading the [manual](http://php.net/manual/en/function.checkdate.php), and the [tour](http://stackoverflow.com/tour) – Geoff Atkins Sep 02 '15 at 15:59
  • http://php.net/manual/en/datetime.createfromformat.php then check http://php.net/manual/en/datetime.getlasterrors.php – AbraCadaver Sep 02 '15 at 15:59
  • iam a beginner in php.I dont know about regex.Help me to solve this problem without using regex – Jasmel Pc Sep 02 '15 at 15:59
  • possible duplicate of [Correctly determine if date string is a valid date in that format](http://stackoverflow.com/questions/19271381/correctly-determine-if-date-string-is-a-valid-date-in-that-format) – aimme Sep 02 '15 at 16:05

3 Answers3

1

You could try with date_parse.

Returns array with information about the parsed date on success or FALSE on failure.

A valid date should be in a valid strtotime() format.

Diego Ferri
  • 2,657
  • 2
  • 27
  • 35
1

Use strptime() function to parse your string.

strptime() returns an array with the date parsed, or FALSE on error. if "mm/dd/yyyy" is the date..,then the format is *"%m/%d/%Y"*.

then strptime() can be used like this..,

strptime ("mm/dd/yyyy" , "%m/%d/%Y" );


if the string is in correct format..,it returns true else return false


Jasmel Pc
  • 515
  • 1
  • 5
  • 16
0

date_parse() will do what you need it to do - but you need to check the output to ensure that the date is indeed a valid or invalid date. The date is valid if the warnings and errors is empty:

<?php

$invalid_date = date_parse("02/30/2015"); //Feburary 30th doesn't exist!
$valid_date = date_parse("02/17/2015");
$wtf_date = date_parse("99/55/XXXX");

function check_date($date){
    if(count($date['warnings']) != 0 || count($date['errors']) != 0){
        return false;
    } else {
        return true;
    } 
}

var_dump(check_date($invalid_date)); //outputs false
var_dump(check_date($valid_date)); //outputs true
var_dump(check_date($wtf_date)); //outputs false

As a bonus, you can see what the error was by looping through the warnings and errors if they exist:

$invalid_date = date_parse("02/30/2015");

if(count($invalid_date['warnings']) != 0 || count($invalid_date['errors']) != 0){
    foreach($invalid_date['warnings'] as $warning){
        //A non-existant day is a warning, not an error.
        echo $warning . PHP_EOL;
    }

    foreach($invalid_date['errors'] as $errors){
        //$wtf_date would have lots of errors.
        echo $errors . PHP_EOL;
    }
} else {
    echo '$invalid_date is valid'  . PHP_EOL;;
}

outputs the following:

$invalid_date: The parsed date was invalid

HPierce
  • 7,249
  • 7
  • 33
  • 49