Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Date Verifying

Posted on 2005-03-08
9
Medium Priority
?
337 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
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
Independent Software Vendors: 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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

564 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