Date format validation in PHP

Advertisement

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

Popularity: 11% [?]

Tags:

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» Solving time difference between hosting server and local timezone in PHP
» Finding difference of days between two dates in PHP
» Ip address validation in PHP using regular expression
» Date or Time Comparison in PHP

10 Comments on “Date format validation in PHP”

  • Suresh wrote on 12 May, 2008, 19:09

    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.

  • Roshan wrote on 13 May, 2008, 10:55

    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..

  • Anand Acharya wrote on 25 November, 2008, 5:35

    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

  • ben wrote on 25 February, 2009, 17:13

    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!

  • Bogdan wrote on 5 May, 2009, 18:10

    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

  • Joe wrote on 22 May, 2009, 23:02

    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);

  • Joe wrote on 22 May, 2009, 23:07

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

  • Bogdan wrote on 24 May, 2009, 10:20

    Thanks

  • pooja wrote on 18 May, 2010, 9:14

    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….

  • decade wrote on 24 June, 2011, 9:48

    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;
    }
    }

Write a Comment

 


Copyright © 2013 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.