从日期添加和删除一年

时间:2013-11-02 01:29:46

标签: php date

我想从日期添加和删除一年

我有这段代码:

$yearbefore = date("Y",strtotime("-1 year"));
$yearnext = date("Y",strtotime("+1 year"));

这将从当前日期添加和删除一年,但如果我想在另一年中使用var?

$year = "2010";

如何做到这一点?

2 个答案:

答案 0 :(得分:1)

您也可以使用strtotime,但默认情况下需要设置日期和月份 - 因为您只需要应该没有问题的年份。

$year = intval($year); //make sure it is an integer
$yearbefore = date("Y", strtotime("01/01/$year -1 year"));
$yearafter  = date("Y", strtotime("01/01/$year +1 year"));

但是当你确定,给定的年份已经是正确的格式,你可以减少/增加它。

$year = intval($year);
$yearbefore = $year - 1;
$yearafter  = $year + 1;

答案 1 :(得分:0)

$date = '2013-11-01';
$seconds_in_year = 31536000;
echo date('y-m-d', strtotime($date) - $seconds_in_year);