code snippet: isDate() mm/dd/yyyy

Include file is found in this directory of the website:

/web/includes/generalFormFuncs.php

Usage Example

if ( strlen($dateOfPurchase) > 0 and !isDate($dateOfPurchase) ) { $isAnomaly = true; }

Date Format:

mm/dd/yyyy

Code For isDate Function:

function isDate($i_sDate) {
/*
function isDate
boolean isDate(string)
Summary: checks if a date is formatted correctly: mm/dd/yyyy (US English)
Author: Laurence Veale (modified by Sameh Labib)
Date: 07/30/2001
*/

  $blnValid = TRUE;
  
  if ( $i_sDate == "00/00/0000" ) { return $blnValid; }
  
  // check the format first (may not be necessary as we use checkdate() below)
  if(!ereg ("^[0-9]{2}/[0-9]{2}/[0-9]{4}$", $i_sDate)) {
    $blnValid = FALSE;
  } else {
    //format is okay, check that days, months, years are okay
    $arrDate = explode("/", $i_sDate); // break up date by slash
    $intMonth = $arrDate[0];
    $intDay = $arrDate[1];
    $intYear = $arrDate[2];
    
    $intIsDate = checkdate($intMonth, $intDay, $intYear);
    
    if(!$intIsDate) {
      $blnValid = FALSE;
    }
  }//end else
  
  return ($blnValid);
} //end function isDate

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in PHP Code Snippets, PHP Data Value Types, PHP Form Processing, PHP Time Functions and tagged , , , . Bookmark the permalink.

Comment