PHP:在时间戳上添加年份

One*_*ema 16 php time timestamp date leap-year

在给定UTC时间戳的PHP中,我想准确添加N年.这应该考虑到闰年.

谢谢.

Jef*_*ker 51

$newTimestamp = strtotime('+2 years', $timestamp);
Run Code Online (Sandbox Code Playgroud)

根据需要更换"+ 2年".

参考:http://php.net/manual/en/function.strtotime.php


Mar*_*c B 8

$date = new DateTime();
$date->add(new DateInterval('P10Y'));
Run Code Online (Sandbox Code Playgroud)

将"10年"(10Y)添加到"今天".但是DateTime只在PHP 5.3中使用.