Date validation issue

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hi guys

I'm having a hard time trying to validate some dates.
I have a form that accept dates from users in this format (YYYY-mm-dd).
I've been looking at the available datetime functions and I'm still
clueless as to how they can help me achieve my goals.

what I need to do id check is:
1- convert the input string into date first. At this point I couldn't
find any srtTOdate type of functions. then I tried doing it the hard using "checkdate" function. I could split the string fine
(using "explode") but then I was stuck because I couldn't find any
function to convert a string into Integer!

2- I need to check if the date is valid

Could somebody please help me out?


Re: Date validation issue wrote:
Quoted text here. Click to load it

using checkdate() sounds good to me. converting a string into an int
works like that:

$int = (int)$string;


Re: Date validation issue


on 06/27/2005 01:04 AM said the following:
Quoted text here. Click to load it

You may want to take a look at this popular forms generation and
validation class that comes with custom input plug-in that lets the user
enter a date that is validated either on the client side with Javascript
and the server side with the class PHP code.

Not only it can validate the date, but also impose optional requirements
to accept dates after a given start date or before a end date.

Here is a screenshot of an example date form:


Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP /

PHP Reviews - Reviews of PHP books and other products /

Metastorage - Data object relational mapping layer generator

Re: Date validation issue wrote:
Quoted text here. Click to load it

function is_valid_date($datestring) {
  if (date('Y-m-d', strtotime($datestring)) == $datestring) {
    return true;
  } else {
    return false;

Usage examples:

is_valid_date('2005-05-22'); // returns true
is_valid_date('2005-05-32'); // returns false

The function assumes leading zeros are mandatory.  If they
are not, here's a revision that will fix it:

function is_valid_date($datestring) {
  list($y, $m, $d) = explode('-', $datestring);
  list($yy, $mm, $dd) = explode('-', date('Y-m-d',
  if (($y == $yy) and ($m == $mm) and ($d == $dd)) {
    return true;
  } else {
    return false;


Re: Date validation issue

*** wrote/escribió (26 Jun 2005 21:04:47 -0700):
Quoted text here. Click to load it

In the "Date and Time Functions" chapter of the PHP manual you have all
date and time functions, including strtotime()

 then I tried doing it the hard
Quoted text here. Click to load it

As you already mentioned, checkdate sounds like a good option.

You may also find useful this code for a previous additional client-side

 * Returns true if valid date, false otherwise
 * Month range is [1,12]
function is_date(day, month, year // v2005-03-14
    var TheDate=new Date();
    TheDate.setFullYear(year, month-1, day);

    return (TheDate.getDate()==day) && (TheDate.getMonth()==month-1) &&

-- Álvaro G. Vicario - Burgos, Spain
-- - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group

Re: Date validation issue

Thanks for all the replys
 It's a lot of help.

Site Timeline