Mar 2008
12
09:10pm

Here’s a nifty little script I use to validate date input with PHP. This function can accept any common date entry and return true if the date is valid or false otherwise.

The Script

<?php

function isValidDate ($dateString) {
        $x = strtotime ($dateString);
        if ($x === false || $x == -1) {
                return false;
        } else {
                return true;
        }
}

/* usage */
isValidDate ("March 18, 1977");  // returns true
isValidDate ("tomorrow");  // returns true
isValidDate ("+1 Month");  // returns true
isValidDate ("Invalid Date Here"); // returns false;

?>

Since this isValidDate uses PHP’s strtotime function, it can practically accept any English textual datetime description.


Share this Post through Social Bookmarking These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Related Posts

Tags:

Leave a Reply