?
Solved

Date Verifying

Posted on 2005-03-08
9
Medium Priority
?
336 Views
Last Modified: 2008-03-10
Hi,
   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:-

$dateparts=explode("-",$_POST['date']);

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:

12.5-6-1985

or

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,
                           Chisel

P.S. Hope I am clear enough
0
Comment
Question by:Chisel
[X]
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
9 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 300 total points
ID: 13488408
What about:

$date = $_POST['date'];

if(date('d-m-Y', strtotime($date)) == $date)
{
//okee
}
else
{
//failed
}

-r-
0
 
LVL 5

Expert Comment

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

Try this link:

http://javascript.internet.com/forms/format-date.html

S:
0
 

Author Comment

by:Chisel
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:-

12-12-2005
01-01-1970    (This being what was generated by your code, not what I'd expect).
0
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

by:Chisel
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.
0
 

Author Comment

by:Chisel
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:

28-05-2018
0
 
LVL 49

Expert Comment

by:Roonaan
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:

Use:
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

-r-
0
 
LVL 3

Assisted Solution

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

http://www.gnu.org/software/tar/manual/html_chapter/tar_7.html


      

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
24-sep-72
24sep72
0
 

Author Comment

by:Chisel
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.
0
 

Author Comment

by:Chisel
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.
0

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