Page 1 of 1
different date format validations ??
Posted: Tue Jul 15, 2008 4:47 am
by PHPycho
hello forums!!
can anybody give idea on different types of date format validation function like Y-m-d , dmY etc.
example:
Code: Select all
function validateDate($input_date, $format){
//necessary processings....
return bool;
}
validateDate('2008-08-135', 'Y-m-d');
Thanks in advance for the valueable suggestions
Re: different date format validations ??
Posted: Tue Jul 15, 2008 5:04 am
by VladSun
You may use the
http://bg2.php.net/manual/en/function.strtotime.php
time
The string to parse, according to the GNU » Date Input Formats syntax. Before PHP 5.0.0, microseconds weren't allowed in the time, since PHP 5.0.0 they are allowed but ignored.
If it returns false, then the given date is not valid or not in valid format.
Re: different date format validations ??
Posted: Tue Jul 15, 2008 5:15 am
by VladSun
Code: Select all
return strtotime(date($format, strtotime($input))) == strtotime($input);
This one also validates whether the user has followed the required date format.
Re: different date format validations ??
Posted: Tue Jul 15, 2008 5:28 am
by alex.barylski
Personally I try and deal with dates in terms of intergers only...it makes comparison faster and less headaches.
If you are accepting a formatted date with a FORM input control you might want to reconsider and use a date picker widget or series of comboboxes -- if accessibility (no script) is important to you.
This way you can assume the input to be of a certain format and just change it to an integer to check it's validity.