Chisel
asked on
Date Verifying
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("-",$_P OST['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
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("-",$_P
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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).
$dateparts="12-12-2005" //As would be passed from my form
printf("%s",$dateparts); //Show the 'passed' value
printf("%s",date('d-m-Y',$
This gave me the result:-
12-12-2005
01-01-1970 (This being what was generated by your code, not what I'd expect).
ASKER
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.
ASKER
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
28-05-2018
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',s trtotime($ 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-
Use:
printf("%s",date('d-m-Y',s
Instead of
printf("%s",date('d-m-Y',$
-r-
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
No I just need to implement this in my code to make sure it works there.
ASKER
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.
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.
Try this link:
http://javascript.internet.com/forms/format-date.html
S: