r/PHPhelp • u/[deleted] • Jan 10 '17
Best method for validating a date?
I have a form that will submit dates as "yyyy-mm-dd" if the user uses the date picker, but if they go ahead and enter their date manually, I need to catch this (I think).
I'm stuck with this hacked together function:
It works. But I'm just questioning it a little.
The date is destined for a MySQL TIMESTAMP field, so I could just pass the data to that without validating, but I want to give the user a readable error if they're putting in something that won't work.
Thoughts?
1
u/Mike312 Jan 10 '17 edited Jan 10 '17
Disregard this, the other guy posted the right answer for what you're doing.
1
Jan 10 '17
Hmm... I'm not sure about that, it even returns "ABC" as a valid date! :)
1
u/Mike312 Jan 10 '17
Oh, nevermind that, it forces the date to be valid, you're right. I'm almost back at a desktop, I'll edit it
1
1
u/omniuni Jan 11 '17
I actually usually do validation in JS and then run it through strtotime, which is pretty amazing. Even if the JS fails, PHP figures it out most of the time, and at least I know it's a valid date of some sort. Also, a recommendation: always store your dates as a Unix timestamp. It's short, simple, time zone neutral, and every single language can work with then easily.
1
Jan 11 '17
I'm using a JS date picker already. If they stick to that, then all should be good (I assume). So this is for the even that they avoid using that, which could be either for input speed (which would still be able to be validated in JS) or if JavaScript happened to be disabled (which would kill the date picker and the validation mechanism as well).
I also looked at unix timestamps, and maybe I'll switch that that in the future. I'm probably just lazy, but it seems like it takes extra manipulation to render the date in a readable format, whether through PHP or browsing the database directly.
1
Jan 11 '17
Thoughts?
You can't reliably validate a random string into a date. There are just to many weird ways to note them. I wouldn't use an input field for the date but the date picker showing the date in a div only. Or add three input fields telling the user what to input where.
Unfortunately type="date" is only supported by modern browsers.
1
Jan 11 '17
I'm using a input field with a placeholder that's shows what the expected input is, along with a pop up date picker. Reason is, for me I HATE being made to use the mouse unnecessarily. So I just want a method to reject invalid inputs if the user opts for keyboard entry, and give them a human readable error.
3
u/notian Jan 10 '17
The simplest solution is to use strtotime and check that the return isn't false.
You could also do a new DateTime in a try/catch block and respond with an error if it throws an exception.
public function getValidDate($string){
try{
$date = new DateTime( $string );
}catch( Exception $e ){
return false;
}
/* Do some more date validation on the object */
return $date->format( 'Y-m-d' );;
}
public function getValidDate($string){
if(false !== ($date = strtotime( $string )){
return date('Y-m-d', $date );
}
return false;
}2
u/the_alias_of_andrea Jan 10 '17
strtotimeis quite loose and accepts a lot of date formats. I think that could go wrong here, if guesses the order of the fields wrong.It'd be better to validate against the specific date format being used here.
2
u/notian Jan 10 '17
The fact that it is loose is an up-side to me. If the user enters mm/dd/yyyy or yyyy/mm/dd then it will be accepted and understood, and only in a small number of cases result in flipped month/day values (i.e. when the day <12 it becomes less predictable).
1
2
u/lchoate Jan 10 '17
If you can pull in another library, look into carbon/carbon. It is a life saver.
1
u/holyshock Jan 10 '17 edited Jan 10 '17
Always validate data, even if you're sure the user will always use datepicker. If you are sure user will always submit with YYYY-MM-DD format, then something like this is the best approach...
$userSubmittedDate = "2017-01-01";
if (date("Y-m-d", strtotime($userSubmittedDate)) !== $userSubmittedDate) {
//problem...
} else {
// fine!
}1
u/preinheimer Jan 11 '17
So I use a multi-step approach, demo: http://recordit.co/GvoFm6bDRq
I send the contents of my date field using ajax to the server, which runs
strtotimeon it, and returns the date in a common formatI update the text field to YYYY-MM-DD from that dat
I display that date underneath in a more human readable format under the text box.
I like this approach because strtotime can be confused (some dates are ambiguous), and this way the user sees what they're getting.
2
u/colshrapnel Jan 10 '17