将年份添加到日期重置为1970-01-01 [英] Adding years to a date resets to 1970-01-01

查看:191
本文介绍了将年份添加到日期重置为1970-01-01的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$somedate = "1980-02-15";
$otherdate = strtotime('+1 year', strtotime($somedate));
echo date('Y-m-d', $otherdate);

输出

1981-02-15

$somedate = "1980-02-15";
$otherdate = strtotime('+2 year', strtotime($somedate));
echo date('Y-m-d', $otherdate); 

输出

1982-02-15

$somedate = "1980-02-15";
$otherdate = strtotime('+75 year', strtotime($somedate));
echo date('Y-m-d', $otherdate); 

输出

1970-01-01

如何修复?

推荐答案

这是 2038 bug ,就像y2k由于32位限制,系统无法处理该年以后的日期。请改用 DateTime类,这样可以解决这个问题。

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.

对于PHP 5.3 +

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

对于PHP 5.2

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

这篇关于将年份添加到日期重置为1970-01-01的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆