PHP:向时间戳添加年份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5172849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Adding years to a timestamp
提问by Onema
In PHP given a UTC timestamp I would like to add exactly N number of years. This should take into consideration leap years.
在 PHP 中给出了一个 UTC 时间戳,我想准确地添加 N 年。这应该考虑到闰年。
Thank you.
谢谢你。
回答by Jeff Parker
$newTimestamp = strtotime('+2 years', $timestamp);
Replace "+2 years" as required.
根据需要替换“+2 年”。
回答by Marc B
$date = new DateTime();
$date->add(new DateInterval('P10Y'));
adds 10 years (10Y
) to "today". DateTime's only in PHP 5.3, though.
将 10 年 ( 10Y
)添加到“今天”。但是,DateTime 仅在 PHP 5.3 中可用。
回答by George D.
One thing you should consider when you do this.
执行此操作时应该考虑的一件事。
$newTimestamp = strtotime('+2 years', $timestamp);
This adds up 2 years ( 720 or 721 days). In case you just want to keep the same day and month and add 2 extra years in the timestamp
这加起来是 2 年(720 或 721 天)。如果您只想保持相同的日期和月份并在时间戳中添加 2 年
you have to use mktime.
你必须使用 mktime。
Example
例子
$timestamp = mktime(0, 0, 0, $month, $day, $year+2);`
回答by asd
$date = "1998-08-14";
$newdate = strtotime ( '+2 years' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
echos
回声
2000-08-14