Date Verifying

Posted on 2005-03-08
Medium Priority
Last Modified: 2008-03-10
   I have a form that posts to a php page to verify some data for me. What I wanted help with was the verification of a date. The the form suggests to enetr the date in the form dd-mm-yyyy on entry, but the field is not restricted to allow a formatted entry (as I don't know how to do this if it's possible). I want to check that that each part is within the valid range, and also that the whole string is not completely wrong.

I realise I can use the following:-


if (dateparts[0] <= 31 && dateparts[1] <= 12 && ....)

or similar to that, but will this handle errors if the user were to enter something silly for the date, such as:



January 6th 2000

I'm not worried about checking about invalid dates such as 30-02-2005. Just for completely incorrectly formatted data.

Thanks in advance,

P.S. Hope I am clear enough
Question by:Chisel
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 49

Accepted Solution

Roonaan earned 300 total points
ID: 13488408
What about:

$date = $_POST['date'];

if(date('d-m-Y', strtotime($date)) == $date)


Expert Comment

ID: 13489161
You can write a JS function to validate that date entry value.

Try this link:



Author Comment

ID: 13489310
I tried your code Roonann, but couldn't get it to work in with my code, so I did a little test with it and got the following results, am I doing something wrong?

$dateparts="12-12-2005"  //As would be passed from my form

printf("%s",$dateparts);   //Show the 'passed' value
printf("%s",date('d-m-Y',$dateparts));     //Show the date after formatting with your code

This gave me the result:-

01-01-1970    (This being what was generated by your code, not what I'd expect).
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13489326
prsupriya, I would like to try and keep my code as plain PHP as much as possible, rather than rely on client side javascript operations, as I want my code to be as platform independant as possible.

Author Comment

ID: 13489338
Also, with your code Roonaan, I realised I left out the strtotime part in that example, but I used both, with the strtotime part added in the date was also wrong, and gave:

LVL 49

Expert Comment

ID: 13489344
date accepts a timestamp as second parameter. $dataparts is a date string. Therefor you have to use strtotime to interpret the date string:

printf("%s",date('d-m-Y',strtotime($dateparts)));     //Show the date after formatting with your code
Instead of
printf("%s",date('d-m-Y',$dateparts));     //Show the date after formatting with your code


Assisted Solution

theevilworm earned 200 total points
ID: 13489489
strtotime only accepts a few date formats:



1972-09-24     # ISO 8601.
72-9-24        # Assume 19xx for 69 through 99,
               # 20xx for 00 through 68.
72-09-24       # Leading zeros are ignored.
9/24/72        # Common U.S. writing.
24 September 1972
24 Sept 72     # September has a special abbreviation.
24 Sep 72      # Three-letter abbreviations always allowed.
Sep 24, 1972

Author Comment

ID: 13489681
Thanks, after changing 'd-m-Y' to 'Y-m-d' as suggested by theevilworm, and having PHP rearrange the passed date into that form the 2 answers now come out correctly in my test code.

No I just need to implement this in my code to make sure it works there.

Author Comment

ID: 13489746
Thanks, it works great.

I've split the points between Roonaan and theevilworm, as I used the code provided by Roonaan, but modifed to accept the date properly as suggested by theevilworm.

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question