I've got to validate numerous dates with my current project. Unfortunately, these dates can vary wildly. Examples include:
Dates will not exceed 9999 b.c., nor will they be future (beyond 'today'). What would be the best way to validate that the values submitted are indeed dates, and proper dates at that?
Updates...
All dates must be sortable within their global list. Meaning dates 1 and 3 above must be comparable to one-another, and sorted ASC or DESC.
I'm fully aware of the calendar-changes that have taken place in the past, and the confusion around these changes. My project assumes the user has already performed the proper calibration to find out the date according to our modern-calendar system. I won't be performing this calibration for them.
The validateDate() function checks whether the given string is a valid date using PHP. It uses PHP DateTime class to validate date based on the specified format. This function returns TRUE if date string is valid, otherwise FALSE. $date – Required.
PHP checkdate() Function var_dump(checkdate(2,29,2003)); echo "<br>"; var_dump(checkdate(2,29,2004));
How about a series of carefully-written regular expressions that recognize each possible format. Once you know the format, you can validate and perhaps put it into a uniform representation (e.g., 64-bit time_t).
e.g.,
/(\d{4})-(\d{2})-(\d{2})/
/(\d+)(bc|b.c.|bce|b.c.e)/i
etc.
Since it sounds like each form has its own validation rules, and you're not implementing any widely-available standard, I think you're stuck validating each case separately.
Update:
All dates must be sortable within their global list.
It seems to me that in order to be able to sort dates that appear in different formats you would need a uniform representation for each one internally, as I mentioned before. For example, use a multi-key dictionary (std::multimap in C++, not sure about PHP) to store (uniform representation)->(input representation) mappings. Depending on the implementation of the container, you may get reverse lookups or key ordering for free.
What about using Zend_Date. Zend's date library is a very good date utility library. It can work standalone or with other Zend Libraries and can work with date_default_timezone_set() so dates are automatically parsed for the set timezone and it will work for dates outside of the Unix timestamp range. It can be a little long-winded to write sometimes, but it's strengths greatly outweigh its weaknesses.
You may have to implement your own custom parsing for BC/AD as I'm not sure it would work for that, but it might be worth a try.
Pear also has a date library that might be worth looking at, however, I haven't used it and have heard from a lot of people that they prefer Zend_Date to Pear's Date package.
You could always write your own, but why re-invent the wheel. If it doesn't roll the way you want, take it and improve upon it ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With