date format validation YYYY-MM-DD MM-DD-YYYY DD/MM/YYYY

date validation

Date validation allows validating a date against a format string, minimum/maximum date values and value of other fields (compared fields need to be date validated too).

The key to date validation is the format string. To create a format string, use the following date and/or time elements combined with some delimiter characters:

  • YYYY: year, 4 digits
  • YY: year, 2 digits
  • M: month, 1 or 2 digits (MM for 2 mandatory digits)
  • D: days, 1 or 2 digits (DD for 2 mandatory digits)
  • h: hour, 1 or 2 digits (hh for 2 mandatory digits)
  • m: minute, 1 or 2 digits (mm for 2 mandatory digits)
  • s: seconds, 1 or 2 digits (ss for 2 mandatory digits)

Using these elements you can build most of the numeric date formats used in the world. You can use any kind of delimiters in the format string.

The input is validated syntactically first (number of digits must be correct), then comes the validation against the following rules (if the element exists):

  • 1 <= month <= 12
  • 1 <= days <= 31
  • days must be less or equal than 28, 29, 30 or 31 depending on (leap) year and month
  • 0 <= hours <= 23
  • 0 <= minutes <= 59
  • 0 <= seconds <= 59 

Furthermore, you can specify whether the date is required or not (not required date fields may be left empty).

Date validation is Y2K38 aware: this platform dependent overflow problems of future dates (from 2038) using the DateTime class available since PHP 5.2. This feature gracefully degrades: Clonefish notifies the users with a message about the limitation of date validation on systems where DateTime class is not available, or in other words: users are not allowed to enter overflowing future dates when date validation is used.


<?php 

$config 
= Array(   

  
'element' => Array(   

    
'type' => '...',   
    
// ...further element settings here...   

    
'validation' => Array(    

      Array(
        
'type'     => 'date',
        
'required' => 1// default: 1
        
'format'   => 'formatstring'// default: 'YYYY-MM-DD'
        
'minimum'  => -2125999898// minimum date(+time) as UNIX timestamp
                                   // this value is 1902-08-19
        
'maximum'  => time(),      // maximum date(+time) as UNIX timestamp

        // compare to other fields (both fields need to have at least date
        // format validation set to be comparable)
        
'lessthan'      => 'fieldname'
        
'lesseqthan'    => 'fieldname'
        
'greaterthan'   => 'fieldname'
        
'greatereqthan' => 'fieldname'

        
'help'     => 'Help message value for this validator only'
      
)
    )
  )
);

?>


Live examples:

form validation for developers!

clonefish is a proven, mature form generator class that helps PHP developers to create, validate and process secure, flexible and easy-to-maintain forms
learn more
Bookmark and Share