-1

In my project I am importing users from a csv file. In that some columns have date values Eg:- date of birth, project start date, project dead line date etc. I know the date column headers but the user can enter date format in different ways. How can I validate the value get from csv is a valid date or not?

2
  • 7
    please point out which of the 3177 results for stackoverflow.com/search?q=validate+date+php you have already tried and why you still needed to ask your question.
    – Gordon
    Mar 30, 2011 at 8:52
  • 1
    On a sidenote, your problem shouldn't be how to validate the dates when getting them out of the csv file, but how make sure they dont get in the csv file in those various formats in the first place. After all, how can you ever reliably say which date this is: 11/10/09? Fix the root cause, not the symptoms.
    – Gordon
    Mar 30, 2011 at 9:02

3 Answers 3

1

While not always accurate, you can try strtotime(). It's not a perfect solution but it's worth trying. Just read the notes because it can have varying behavior depending on what your date formats look like.

0

The php function strtotime can take many different date formats and returns a timestamp. If your can make thoughtful assertions on the valid timestamp values, validation can be done with those assertions. But it could be prone to side effects,like day and month mismatch, so it would be a poor validation. I suggest that you enforce a common date pattern on the input file.

0

I use Zend_Date, as the constructor will validate the date for me, but of course if you don't expect a precise format you'll get in the kind problems that @gordon points out with 11/10/09 type of date. This will generate you a valid date, but for a default locale/format which can be of course wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.