PHP: Adding years to a timestamp

白昼怎懂夜的黑 提交于 2019-11-30 06:54:15
$newTimestamp = strtotime('+2 years', $timestamp);

Replace "+2 years" as required.

ref: http://php.net/manual/en/function.strtotime.php

$date = new DateTime();
$date->add(new DateInterval('P10Y'));

adds 10 years (10Y) to "today". DateTime's only in PHP 5.3, though.

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

you have to use mktime.

Example

$timestamp = mktime(0, 0, 0, $month, $day, $year+2);`
asd
$date    = "1998-08-14";

$newdate = strtotime ( '+2 years' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );

echo $newdate;

echos

2000-08-14
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!