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

<span id="more-5"></span>

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
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Tags:

Leave a Reply