How can I work with dates before 1900 in PHP?

后端 未结 2 598
攒了一身酷
攒了一身酷 2020-12-04 02:04

I am using PHP and jQuery to build an interactive timeline which needs to display dates between 1500 and 2020. I usually use PHP\'s strtotime function when work

相关标签:
2条回答
  • 2020-12-04 02:17

    Using the wonderful Carbon Library, dates in the past are not a problem:

    $date = Carbon::now();
    $date->subCenturies(23);
    echo $date->format('Y-m-d');
      // -0282-03-15
    

    This works for dates where humans have been around. For everything else, using a date (with day and month, set on the AC/BC scale) does not make a lot of sense.

    0 讨论(0)
  • 2020-12-04 02:27

    The DateTime class, here, might help (quoting):

    Each component of date (e.g. year) is internally stored as 64-bit number so all imaginable dates (including negative years) are supported.


    But note that:

    • It's only exists in PHP >= 5.2
    • And several methods only exist in PHP >= 5.3

    So: beware of which methods you're using, if you're developping on PHP 5.3 and want your software to be compatible with PHP 5.2


    Another solution (especially, if using Zend Framework in your application) would be the Zend_Date component (quoting):

    Although PHP 5.2 docs state, "The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT," Zend_Date supports a nearly unlimited range, with the help of the BCMath extension

    0 讨论(0)
提交回复
热议问题