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?

albert
  • 8,112
  • 3
  • 47
  • 63
Letter
  • 809
  • 1
  • 6
  • 5
  • possible duplicate of [add one year to datetime with php](http://stackoverflow.com/questions/5212240/add-one-year-to-datetime-with-php) – Rick Hoving Aug 16 '13 at 09: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 '13 at 09:06

12 Answers12

177

Try with:

$end = date('Y-m-d', strtotime('+5 years'));
hsz
  • 148,279
  • 62
  • 259
  • 315
  • 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 '18 at 12:10
  • 1
    what if its 2.5 yrs date – Sayuj3 May 16 '21 at 13:24
  • 2
    @Sayuj3: Use months instead of years. 2.5 years is 30 months. – TheSatinKnight Jul 03 '21 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));
    ?>
Gadoma
  • 6,475
  • 1
  • 31
  • 34
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 -

RN Kushwaha
  • 2,081
  • 3
  • 29
  • 40
8
$date = strtotime($row['timestamp']);
$newdate = date('d-m-Y',strtotime("+1 year",$date));
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Nirav
  • 99
  • 2
  • 9
7

Its very very easy with Carbon.

$date = "2016-02-16"; // Or Your date
$newDate    = Carbon::createFromFormat('Y-m-d', $date)->addYear(1);
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 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 :) – K Arun Singh Nov 18 '16 at 09: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. – Bijaya Prasad Kuikel Jun 07 '20 at 16:17
2

Using Carbon:

$dt = Carbon::now();
echo $dt->addYears(5); 
ciruvan
  • 5,143
  • 1
  • 26
  • 32
1

To add one year to todays date use the following:

$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day"));
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
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');
Tuncay Elvanagac
  • 1,048
  • 11
  • 13
1

Try this code:

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

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

?>
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Divyesh
  • 121
  • 1
  • 7
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;
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
Abhi
  • 1,105
  • 2
  • 12
  • 29
0

try this:

$yearnow= date("Y");
$yearnext=$yearnow+1;
echo date("Y")."-".$yearnext;
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>';
}