PHP: strtotime is returning false for a future date?

前端 未结 5 1796
南方客
南方客 2020-11-29 11:40

here are some debug expressions i put into eclipse, if you don\'t believe me:

\"strtotime(\"2110-07-16 10:07:47\")\" = (boolean) false    
\"strtotime(\"2110         


        
相关标签:
5条回答
  • 2020-11-29 12:04

    You cant convert dates that occur after the unix time rollover (2038)

    0 讨论(0)
  • 2020-11-29 12:05

    Simple replacement of strtotime

    $date = '2199-12-31T08:00:00.000-06:00';
    
    echo date('Y-m-d', strtotime($date)); // fails with 1970 result
    
    echo date_format(  date_create($date) , 'Y-m-d'); // works perfect with 5.2+
    

    Actual post here.

    0 讨论(0)
  • 2020-11-29 12:07

    From the PHP manual:

    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. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).

    See also: Year 2038 problem - Wikipedia

    0 讨论(0)
  • 2020-11-29 12:09

    Try to keep it before Tue, 19 Jan 2038 03:14:07 UTC, when the unix timestamp epoch for 32 bit systems rolls over!

    It's even described in the manual at http://php.net/strtotime

    edit: Just tested: It's fixed by installing a 64 bit OS and appropriate 64 bit version of php. I guess we have time enough to fix a reincarnated millenium bug:

    $one = strtotime("9999-12-31 23:59:59");  
    $two = strtotime("10000-01-01 00:00:00");
    var_dump($one);
    var_dump($two);
    
    int(253402297199)
    bool(false)
    
    0 讨论(0)
  • 2020-11-29 12:11

    If you want to work with dates that fall outside the 32-bit integer date range, then use PHP's dateTime objects

    try {
        $date = new DateTime('2110-07-16 10:07:47');
    } catch (Exception $e) {
        echo $e->getMessage();
        exit(1);
    }
    
    echo $date->format('Y-m-d');
    
    0 讨论(0)
提交回复
热议问题