We have 29 guests online
   
PHP Scripts arrow Free PHP Tutorials arrow Free PHP Tutorials arrow PHP Tutorials and Examples arrow PHP 
Free PHP Tutorials arrow PHP Tutorials and Examples arrow PHP

isDate
User Rating: / 13
PoorBest 

Checks if a string is a valid date in the format dd/mm/yyyy.
Could easily be adapted to check for other formats or the ereg a little less strict i.e. allow 1/8/2001. One improvement could be a second parameter passed in with the actual date format to be checked for(e.g
if(isDate($strDate, $strDateFormat))
{
...
}
PHP functions used: ereg(), explode() and checkdate()

  1. <?php
  2. /***********************************************************************
  3. * function isDate
  4. *
  5. * boolean isDate(string)
  6. * Summary: checks if a date is formatted correctly: dd/mm/yyyy (european)
  7. * Author: Laurence Veale
  8. * Date: 30/07/2001
  9. ***********************************************************************/
  10. function isDate($i_sDate)
  11. {
  12.   $blnValid = TRUE;
  13.    // check the format first (may not be necessary as we use checkdate() below)
  14.    if(!ereg ("^[0-9]{2}/[0-9]{2}/[0-9]{4}$", $i_sDate))
  15.    {
  16.     $blnValid = FALSE;
  17.    }
  18.    else //format is okay, check that days, months, years are okay
  19.    {
  20.       $arrDate = explode("/", $i_sDate); // break up date by slash
  21.       $intDay = $arrDate[0];
  22.       $intMonth = $arrDate[1];
  23.       $intYear = $arrDate[2];
  24.  
  25.       $intIsDate = checkdate($intMonth, $intDay, $intYear);
  26.    
  27.      if(!$intIsDate)
  28.      {
  29.         $blnValid = FALSE;
  30.      }
  31.  
  32.    }//end else
  33.    
  34.    return ($blnValid);
  35. } //end function isDate
  36. ?>

 

 

Directory Stats

There are 882 listing and 46 categories in our website

Change Language