skip to Main Content

i use several datefunction with dates around the French republic of 1792-1806.
But i want to know if the (php8) date function can handle these dates.

function CheckFrDate($CheckDate){
  $BeginFrDate = date_create_from_format('d/m/Y', '22/09/1792');
  $EndFrDate = date_create_from_format('d/m/Y', '22/09/1806');
  if (
    $CheckDate->getTimestamp() > $BeginFrDate->getTimestamp() && 
    $CheckDate->getTimestamp() < $EndFrDate->getTimestamp()){
    return 1;
  }else{
    return -1;  
  } 
}

So a date like 23/09/1793 should be ok. But my function keeps returning false.

2

Answers


  1. If a date function returns false, you are able to get the real (verbose) error message by calling var_dump(date_get_last_errors());

    read more:
    https://www.php.net/manual/en/function.date-get-last-errors.php

    Login or Signup to reply.
  2. I’d dare say that PHP date ranges do not have hard-coded limits, probably just the integer size or some similar platform limit. However, not all input mechanisms work the same way. For example:

    $years = [
        -25000,
        0,
        9999,
        10000,
        25000,
    ];
    foreach ($years as $year) {
        $dt1 = date_create_from_format('d/m/Y', "22/09/$year");
        $dt2 = new DateTime();
        $dt2->setDate($year, 9, 22);
        var_dump($year, $dt1, $dt2);
        echo "n";
    }
    
    int(-25000)
    bool(false)
    object(DateTime)#1 (3) {
      ["date"]=>
      string(28) "-25000-09-22 16:16:09.918321"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(13) "Europe/Berlin"
    }
    
    int(0)
    object(DateTime)#2 (3) {
      ["date"]=>
      string(26) "0000-09-22 16:16:09.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(13) "Europe/Berlin"
    }
    object(DateTime)#3 (3) {
      ["date"]=>
      string(26) "0000-09-22 16:16:09.918367"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(13) "Europe/Berlin"
    }
    
    int(9999)
    object(DateTime)#1 (3) {
      ["date"]=>
      string(26) "9999-09-22 16:16:09.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(13) "Europe/Berlin"
    }
    object(DateTime)#2 (3) {
      ["date"]=>
      string(26) "9999-09-22 16:16:09.918415"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(13) "Europe/Berlin"
    }
    
    int(10000)
    bool(false)
    object(DateTime)#1 (3) {
      ["date"]=>
      string(27) "10000-09-22 16:16:09.918451"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(13) "Europe/Berlin"
    }
    
    int(25000)
    bool(false)
    object(DateTime)#2 (3) {
      ["date"]=>
      string(27) "25000-09-22 16:16:09.918476"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(13) "Europe/Berlin"
    }
    

    In particular, date_create_from_format() reports this:

    date_create_from_format('d/m/Y', "22/09/25000");
    print_r(DateTime::getLastErrors());
    
    Array
    (
        [warning_count] => 0
        [warnings] => Array
            (
            )
    
        [error_count] => 1
        [errors] => Array
            (
                [10] => Trailing data
            )
    
    )
    

    This happens because, as documented, Y format code is:

    A full numeric representation of a year, up to 4 digits

    Regarding your specific code, it appears to work as expected:

    function CheckFrDate($CheckDate){
        $BeginFrDate = date_create_from_format('d/m/Y', '22/09/1792');
        $EndFrDate = date_create_from_format('d/m/Y', '22/09/1806');
        if (
            $CheckDate->getTimestamp() > $BeginFrDate->getTimestamp() &&
            $CheckDate->getTimestamp() < $EndFrDate->getTimestamp()){
            return 1;
        }else{
            return -1;
        }
    }
    
    $CheckDate = date_create_from_format('d/m/Y', '23/09/1793');
    var_dump(CheckFrDate($CheckDate));
    
    int(1)
    

    I no longer have a 32-bit Windows system to test but, if I recall correctly, DateTime interface was never affected by the 1970 Unix Epoch limit. However, if you use ->getTimestamp() to obtain a Unix timestamp, you’ll get an integer that will be.

    You can drop Unix time altogether:

    function CheckFrDate($CheckDate)
    {
        $BeginFrDate = date_create_from_format('d/m/Y', '22/09/1792');
        $EndFrDate = date_create_from_format('d/m/Y', '22/09/1806');
        if ($CheckDate > $BeginFrDate && $CheckDate < $EndFrDate) {
            return 1;
        } else {
            return -1;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search