Adding years to a date resets to 1970-01-01

后端 未结 5 1008
悲&欢浪女
悲&欢浪女 2020-12-11 09:47
$somedate = \"1980-02-15\";
$otherdate = strtotime(\'+1 year\', strtotime($somedate));
echo date(\'Y-m-d\', $otherdate);

outputs

19         


        
5条回答
  •  执笔经年
    2020-12-11 10:33

    It's the 2038 bug which is like y2k where systems can't handle dates after that year due to 32 bit limitations. Use the DateTime class instead which does work around this issue.

    For PHP 5.3+

    $date = new DateTime('1980-02-15');
    $date->add(new DateInterval('P75Y'));
    echo $date->format('Y-m-d');
    

    For PHP 5.2

    $date = new DateTime('1980-02-15');
    $date->modify('+75 year');
    echo $date->format('Y-m-d');
    

提交回复
热议问题