Advanced data validation using PHP

I need to check numerous dates with my current project. Unfortunately, these dates can vary greatly. Examples include:

  • 1983-07-10 (after 1970)
  • 1492-10-11 (Until 1970, Unix Timestamps - this excludes strtotime () for some systems).
  • 200 BC (Really old ...)

Dates will not exceed 9999 bc, and they will not be future (outside the "today"). What would be the best way to verify that the values ​​represented are dates and valid dates?

Updating ...

All dates must be sorted in their global list. The dates 1 and 3 above should be comparable to one another, and also sorted by ASC or DESC.

I am fully aware of the calendar changes that have occurred in the past, and the confusion surrounding these changes. My project assumes that the user has already completed the correct calibration to find out the date according to our modern calendar system. I will not perform this calibration for them.

+5
source share
7 answers

How about a series of carefully written regular expressions that recognize every possible format. Once you know the format, you can check and possibly put it in a single view (for example, 64-bit time_t).

eg.

/(\d{4})-(\d{2})-(\d{2})/
/(\d+)(bc|b.c.|bce|b.c.e)/i
etc.

, , , , , .

Update:

.

, , , , , , . , (std:: multimap ++, PHP) ( ) → ( ) . .

+5

Zend_Date. Zend date library - . Zend date_default_timezone_set(), , Unix. , , .

, BC/AD, , , , , .

, , , Zend_Date Pear Date.

, . , , ;)

+2

DateTime. , , , BC/AD, .. , Money, .

, , - , 200 . 1492-10-07 , . , BC < 0 < AD, .

+1

, , , year/month/day ( ... :). , .

, ... () : checkdate(). >= 1, .

, , , year <= 0.

side-trek , BIG...

, 45 . , . , ; , 4 1582 , , , 15 1582 ( ).

, 5 1582 14 1582 , ; .

, 45 . .. 46 . .. .

, , , , " ". - , .

, , , , , BC 1/1 . , /, "BC", BC AD - .

, , - . , 8 AD.

, 45 () . is-year-leap, , julian/gregorian:

define('YEAR_JULIAN_CALENDAR_INTRODUCED', -45);
define('YEAR_JULIAN_CALENDAR_LEAP_IMPLEMENTED_CORRECTLY', 8);
define('YEAR_GREGORIAN_CALENDAR_INTRODUCED', 1582);

function is_leap_year($year) {
    if($year < YEAR_JULIAN_CALENDAR_INTRODUCED) {
        return false; // or good luck :)
    }
    if($year < YEAR_JULIAN_CALENDAR_LEAP_IMPLEMENTED_CORRECTLY) {
        return $year <= -9 && $year % 3 == 0;
    }
    if($year < YEAR_GREGORIAN_CALENDAR_INTRODUCED) {
        return $year % 4 == 0;
    }
    // Otherwise, Gregorian is in effect
    return $year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0);
}

, , , . / .

( , ). :

?

, " ", , : a) , b) , 3- , , .

, , .:)

+1

, , ( - ) - .

0

, , :

- , 9999 . .. ( ).

1/1/10000 .. ( ); 64 . .

. 64- int PHP.

PHP 31 . :

  • 62- , . 62 .

    , , . : - PHP.

  • BCMath GMP .

    , . , . : .

60-- ( // ), CustomDateTime, . "--int" (, ); -- (, ) .

. 64- int .

All databases do this without a problem. You almost certainly need to go this route because, for example, MySQL does not support dates up to 1000 AD. I do not know other suppliers.

0
source

what about strtotime ()?

-2
source

All Articles