Date format validation in PHP

Today Sushil asked me how can we validate the date which is entered from textbox in “YYYY-MM-DD” format. Well, we can validate the format of the date using regular expression but how to validate weather that date is valid date or not, such as “2007-02-29″ is the correct format of the date but it’s not the valid date.

To overcome that situation, I’ve used checkdate() function available in PHP for validation of date.

Function to validate date format in PHP

function checkDateFormat($date)
{
  //match the format of the date
  if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
  {
    //check weather the date is valid of not
	if(checkdate($parts[2],$parts[3],$parts[1]))
	  return true;
	else
	 return false;
  }
  else
    return false;
}

In the above function, first of all format of date is validated using regular expression. As you can see the in the preg_match() function, there are three expression each separated by “-” and there can be only digits of length of 4,2 and 2 in these expressions. If the date format is incorrect then this function returns “false” value. And, if the supplied string contains the valid date format then the part matching each expression are stored in the “$parts” array. Such as, if we supply “2007-03-12″ then “2007”, “03” and “12” are stored in the “$parts” array. After that, checkdate() function of PHP is used check weather the supplied date is valid date or not.

Example

echo checkDateFormat("2008-02-29"); //return true
echo checkDateFormat("2007-02-29"); //return false

10 thoughts on “Date format validation in PHP

  1. I used checkdate() to validate date in format ‘YYYY-mm-dd’ . but i can’t able to get the expect result . Now i am using Javascript to validate date.

  2. You can’t rely on the validation of javascript, as it can be bypassed by disabling javascript in browser and that function was working perfectly for various supplied data……can you please post the dates from which you’ve got bad or wrong result..

  3. how to validate date in (dd/mm/yyyy) format ?
    i have tried your function with

    if (preg_match (“/^([0-9]{2})/([0-9]{2})/([0-9]{4})$/”, $date, $parts))
    {
    if(checkdate($parts[2],$parts[1],$parts[3]))
    return true;
    else
    return false;
    }

    but is gives an error Unknown modifier ‘(‘ in preg_match

  4. ben

    if (preg_match (”/^([0-9]{2})/([0-9]{2})/([0-9]{4})$/”, $date, $parts))
    {
    if(checkdate($parts[2],$parts[1],$parts[3]))
    return true;
    else
    return false;
    }

    in reply to the above poster, to make this function work you will need to slash out the slashes to make PHP recognise them… this will work…

    if (preg_match (”/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/”, $date, $parts))
    {
    if(checkdate($parts[2],$parts[1],$parts[3]))
    return true;
    else
    return false;
    }

    Hope this helps!

  5. Bogdan

    Can anyoane help me to validate a date and time field? The format is yyyy-mm-dd HH:mm:ss (ex: 2009-05-18 20:59:50)

    Thanks

  6. Joe

    Here is a function if you have a string of dates that need to be validated. I also need to pick off the last date, thats why it is sorted. This is a pretty strict function obviously you can tone it down to your needs, most could probably do without checkdate since most date errors will be caught in the preg, and of course you probably don’t need a sort.

    function checkDateFormat(&$data){
    preg_match_all(‘/(20\d{2})\/(0[1-9]|1[012])\/(0[1-9]|[12]\d|3[01])/’, $data, $matches, PREG_SET_ORDER);
    $orderDates=array();
    for ($i = 0; $i < count($matches); $i++) {
    if(checkdate($matches[$i][2],$matches[$i][3],$matches[$i][1])){
    $orderDates[]=strtotime($matches[$i][0]);
    }
    }
    sort($orderDates);
    $output=false;
    for ($i=0;$i<sizeof($orderDates);$i++) {
    if ($i!=0) {
    $output.= ‘,';
    }
    $output.= date(‘Y/m/d’,$orderDates[$i]);
    }
    if($output){
    return $output;
    } else {
    return false;
    }
    }
    $str = ‘2008/02/29,2007/02/29,2004/07/07,2035/13/00,2005/04/25′; // 2008 was a leap year
    echo checkDateFormat($str);

  7. Joe

    ps the above preg only accept years 2000 – 2099, you need to adjust this for your preferences

  8. Bogdan

    Thanks

  9. pooja

    hello.. i am trying to fetch datetime from a text…. can any one help me how can i do this using regular expression preg_match????

    Thnaks….

  10. decade

    damn this i use your preg_match and it give an error so i modify it into more simple code :

    checkDateFormat($date){
    if(preg_match(“/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/”, $date)){
    return true;
    }else{
    return false;
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>