80

I have this PHP code:

$end=date('Y-m-d');

I use it to get the current date, and I need the date 5 years in the future, something like:

$end=date('(Y + 5)-m-d');

How can I do this?

2
  • possible duplicate of add one year to datetime with php Aug 16, 2013 at 9:01
  • Gets slightly quirky on 29th February in a leap year when you suddenly get 1st March - be aware of such oddities when trying to manipulate dates
    – Mark Baker
    Aug 16, 2013 at 9:06

12 Answers 12

177

Try with:

$end = date('Y-m-d', strtotime('+5 years'));
3
  • 3
    Hi, I need to use this example to add 1 year to a date, but I already have variables: $invoicedate = $_POST['invdate']; $invdate = date ("Y-m-d", strtotime($invoicedate)); so would I then add a 3rd line $due = date('Y-m-d', strtotime($invdate,'+1 year'));?
    – Adrian
    Jan 17, 2018 at 12:10
  • 1
    what if its 2.5 yrs date
    – Sayuj3
    May 16, 2021 at 13:24
  • 2
    @Sayuj3: Use months instead of years. 2.5 years is 30 months. Jul 3, 2021 at 21:34
29

Modifying dates based on this post
strtotime() is really powerful and allows you to modify/transform dates easily with it’s relative expressions too:

Procedural

    $dateString = '2011-05-01 09:22:34';
    $t = strtotime($dateString);
    $t2 = strtotime('-3 days', $t);
    echo date('r', $t2) . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100

DateTime

    $dateString = '2011-05-01 09:22:34';
    $dt = new DateTime($dateString);
    $dt->modify('-3 days');
    echo $dt->format('r') . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100

The stuff you can throw at strtotime() is quite surprising and very human readable. Have a look at this example looking for Tuesday next week.

Procedural

    $t = strtotime("Tuesday next week");
    echo date('r', $t) . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100

DateTime

    $dt = new DateTime("Tuesday next week");
    echo $dt->format('r') . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100

Note that these examples above are being returned relative to the time now. The full list of time formats that strtotime() and the DateTime constructor takes are listed on the PHP Supported Date and Time Formats page.

Another example, suitable for your case could be: based on this post

    <?php
    //How to get the day 3 days from now:
    $today = date("j");
    $thisMonth = date("n");
    $thisYear = date("Y");
    echo date("F j Y", mktime(0,0,0, $thisMonth, $today+3, $thisYear)); 

    //1 week from now:
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
    echo date("F j Y", mktime(0,0,0, $thisMonth, $today+7, $thisYear));

    //4 months from now:
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
    echo date("F j Y", mktime(0,0,0, $thisMonth+4, $today, $thisYear)); 

    //3 years, 2 months and 35 days from now:
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
    echo date("F j Y", mktime(0,0,0, $thisMonth+2, $today+35, $thisYear+3));
    ?>
14

Use this code to add years or months or days or hours or minutes or seconds to a given date

 echo date("Y-m-d H:i:s", strtotime("+1 years", strtotime('2014-05-22 10:35:10'))); //2015-05-22 10:35:10
 echo date("Y-m-d H:i:s", strtotime("+1 months", strtotime('2014-05-22 10:35:10')));//2014-06-22 10:35:10
 echo date("Y-m-d H:i:s", strtotime("+1 days", strtotime('2014-05-22 10:35:10')));//2014-05-23 10:35:10
 echo date("Y-m-d H:i:s", strtotime("+1 hours", strtotime('2014-05-22 10:35:10')));//2014-05-22 11:35:10
 echo date("Y-m-d H:i:s", strtotime("+1 minutes", strtotime('2014-05-22 10:35:10')));//2014-05-22 10:36:10
 echo date("Y-m-d H:i:s", strtotime("+1 seconds", strtotime('2014-05-22 10:35:10')));//2014-05-22 10:35:11

You can also subtract replacing + to -

8
$date = strtotime($row['timestamp']);
$newdate = date('d-m-Y',strtotime("+1 year",$date));
7

Its very very easy with Carbon.

$date = "2016-02-16"; // Or Your date
$newDate    = Carbon::createFromFormat('Y-m-d', $date)->addYear(1);
2
  • Carbon class and its methods belongs to a particular frame work (Laravel) and in the question asked we do not find any frame work name rather it is asked using PHP date functions. I think that's why you didn't get any up thumbs :) Nov 18, 2016 at 9:18
  • 2
    Actually carbon is not dependent on particular framework. Anyone can pull in carbon via composer and use it. It is a easier way to deal with the issue if one does not wishes to check core PHP's functionality. Jun 7, 2020 at 16:17
2

Using Carbon:

$dt = Carbon::now();
echo $dt->addYears(5); 
1

To add one year to todays date use the following:

$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day"));
2
  • This was great thank you very much :) i'm accepting a few moments later
    – Letter
    Aug 16, 2013 at 9:02
  • @Letter Look at my answer - it's much simplier.
    – hsz
    Aug 16, 2013 at 9:04
1

You may use DateInterval for this purpose;

$currentDate = new \DateTime(); //creates today timestamp
$currentDate->add(new \DateInterval('P5Y')); //this means 5 Years
and you can now format it;
$currentDate->format('Y-m-d');
1

Try this code:

<?php
$current_date=strtotime(date('Y-m-d'));

echo $end = date('Y-m-d', strtotime('+5 years',$current_date));

?>
0

try this ,

$presentyear = '2013-08-16 12:00:00';

$nextyear  = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($presentyear )),   date("d",strtotime($presentyear )),   date("Y",strtotime($presentyear ))+5));

echo $nextyear;
0

try this:

$yearnow= date("Y");
$yearnext=$yearnow+1;
echo date("Y")."-".$yearnext;
1
  • Not quite what was asked.
    – wonce
    Oct 4, 2016 at 19:44
0

Try this code and add next Days, Months and Years

// current month: Aug 2018
$n = 2;
for ($i = 0; $i <= $n; $i++){
   $d = strtotime("$i days");
   $x = strtotime("$i month");
   $y = strtotime("$i year");
   echo "Dates : ".$dates = date('d M Y', "+$d days");
   echo "<br>";
   echo "Months : ".$months = date('M Y', "+$x months");
   echo '<br>';
   echo "Years : ".$years = date('Y', "+$y years");
   echo '<br>';
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.