Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced Date-Validation with PHP

I've got to validate numerous dates with my current project. Unfortunately, these dates can vary wildly. Examples include:

  1. 1983-07-10 (After 1970)
  2. 1492-10-11 (Before 1970, year of Unix Timestamps - this eliminates strtotime() on some systems)
  3. 200 B.C. (Really old...)

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.

like image 862
Sampson Avatar asked Jul 19 '09 22:07

Sampson


People also ask

How to set date validation in PHP?

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.

How to check date condition in PHP?

PHP checkdate() Function var_dump(checkdate(2,29,2003)); echo "<br>"; var_dump(checkdate(2,29,2004));


2 Answers

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.

like image 84
Tim Sylvester Avatar answered Sep 25 '22 11:09

Tim Sylvester


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 ;)

like image 44
Tres Avatar answered Sep 22 '22 11:09

Tres