Maximum time() | PHP

前端 未结 6 1256
半阙折子戏
半阙折子戏 2020-12-15 18:08

It\'s kind of a silly question, but what would be the maximum INT value of a time() and it\'s future date, e.g.

1st January 2999

相关标签:
6条回答
  • 2020-12-15 18:23

    PHP stores the highest integer number it can represent in the PHP_INT_MAX constant:

    date('Y-m-d H:i:s', PHP_INT_MAX); // 2038-01-19 04:14:07
    

    If you want to work with dates beyond that, consider using the DateTime API, e.g.

    $dt = new DateTime('1st January 2999');
    $dt->add(DateInterval::createFromDateString('+1 day'));
    echo $dt->format('Y-m-d H:i:s'); // 2999-01-02 00:00:00
    echo $dt->format('U');           // 32472226800
    
    0 讨论(0)
  • 2020-12-15 18:30

    DateTime seems to use 32bit on 64bit servers, too. So you get into trouble.

    I've solve it this way:

    new DateTime("99999/12/31 00:00:00");
    

    Because, the date overflows the maximum length for DateTime, date use the maximum possibel value and returns a DateTime-object like this (inspected with var_dump) :

    object(DateTime)#9 (3) { ["date"]=> string(19) "2031-09-12 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" } }
    

    I'm not sure if it differs with the versions of PHP. I've tested it with version 5.4.

    0 讨论(0)
  • 2020-12-15 18:33

    The shortest way I know is to get tomorrow's date:

    date("Y-n-j", strtotime("+1 day"))

    date("Y-n-j", PHP_INT_MAX) on 64bit-systems gives potential dangerous value: 292277026596-12-4

    0 讨论(0)
  • 2020-12-15 18:38

    The last 32-Bit Integer timestamp will be reached January 19, 2038. This is known as the Year 2038 problem.

    0 讨论(0)
  • 2020-12-15 18:45

    Remember, the Y2038 problem does not apply on 64-bit systems.

    0 讨论(0)
  • 2020-12-15 18:46

    On 64-bit platforms PHP_INT_MAX does not reflect maximum INT value for 32-bit platforms. Here's how to get it:

    $max32bitInt = PHP_INT_SIZE == 4 ? PHP_INT_MAX : PHP_INT_MAX>>32;
    

    If you're always using 64-bit platform, just use:

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