116

I'm trying to get a date that is one year from the date I specify.

My code looks like this:

$futureDate=date('Y-m-d', strtotime('+one year', $startDate));

It's returning the wrong date. Any ideas why?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Matt
  • 7,022
  • 16
  • 53
  • 66

14 Answers14

254
$futureDate=date('Y-m-d', strtotime('+1 year'));

$futureDate is one year from now!

$futureDate=date('Y-m-d', strtotime('+1 year', strtotime($startDate)) );

$futureDate is one year from $startDate!

Misho
  • 2,911
  • 2
  • 14
  • 9
  • 1
    Please note that if $startDate is already a timestamp (such as one created earlier with the time() or mktime() function) and not a string, you should not wrap it in strtotime or it will not work. Instead, do `$futureDate=date('Y-m-d',strtotime('+1 year',$startDate));` as K Prime mentioned below. – dallin Sep 04 '18 at 04:11
111

To add one year to todays date use the following:

$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day"));

For the other examples you must initialize $StartingDate with a timestamp value for example:

$StartingDate = mktime();  // todays date as a timestamp

Try this

$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 365 day"));

or

$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 1 year"));
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Nidhin Baby
  • 1,618
  • 4
  • 14
  • 16
  • 1
    Adding "+365" instead of "+1 year" did it. Thanks! – Matt Dec 15 '09 at 16:01
  • 22
    Wouldn't this break in the event of a leap year? – WhoaItsAFactorial Jan 25 '13 at 15:46
  • 1
    As of PHP 5.1, when called with no arguments, mktime() throws an E_STRICT notice: use the time() function instead. – svandragt Jan 29 '14 at 13:54
  • Downvoted here too. The number of seconds in a day, days in a year, seconds in a year, etc. are all variable. If you're interested in 'one year from now', you have to pass in an interval of 'one year'. Subdividing on your own is what leads to errors around leap days and leap seconds and daylight savings changes. Worse, it causes flappy unit tests. – drobert Aug 15 '14 at 18:10
  • why my answer is downvoted ?? $addedoneyear=date("y")+1; //for adding one year whats wrong with it ? – pathe.kiran May 11 '15 at 09:47
  • Downvoted, because it isn't accounting for leap year. – Jose' Vargas Oct 22 '15 at 15:35
  • What about if the next year is a leap year but I want to get the same date for the next year? – Kanan Farzali Sep 28 '17 at 09:34
  • This doesn't even come close to working. There's a typo in `$StartingDate` in two places (calling it `$StaringDate`). Also, calling strtotime on `$StartingDate`, which is _already_ a timestamp since it was created with mktime(), returns false, which makes it return the wrong date. Why are you calling strtotime on a timestamp? Also, mktime() should now be time() as of PHP 5.1. Did this answer ever work for anyone or is it just really outdated? – dallin Sep 04 '18 at 04:18
  • Deprecated: mktime(): You should be using the time() function instead – Ben Summerhayes Nov 10 '20 at 10:27
15
//1 year from today's date
echo date('d-m-Y', strtotime('+1 year'));

//1 year from from specific date
echo date('22-09-Y', strtotime('+1 year'));

hope this simpler bit of code helps someone in future :)

Developer
  • 3,857
  • 4
  • 37
  • 47
9

Try: $futureDate=date('Y-m-d',strtotime('+1 year',$startDate));

K Prime
  • 5,809
  • 1
  • 25
  • 19
7

just had the same problem, however this was the simplest solution:

<?php (date('Y')+1).date('-m-d'); ?>
Shoaib Iqbal
  • 1,208
  • 11
  • 21
Gardenee
  • 115
  • 2
  • 7
7
// Declare a variable for this year 
$this_year = date("Y");
// Add 1 to the variable
$next_year = $this_year + 1;
$year_after = $this_year + 2;

// Check your code
    echo "This year is ";
    echo $this_year;
    echo "<br />";
    echo "Next year is ";
    echo $next_year;
    echo "<br />";
    echo "The year after that is ";
    echo $year_after;
AnarchyOutlaw
  • 163
  • 2
  • 6
7

I prefer the OO approach:

$date = new \DateTimeImmutable('today'); //'today' gives midnight, leave blank for current time.
$futureDate = $date->add(\DateInterval::createFromDateString('+1 Year'))

Use DateTimeImmutable otherwise you will modify the original date too! more on DateTimeImmutable: http://php.net/manual/en/class.datetimeimmutable.php


If you just want from todays date then you can always do:

new \DateTimeImmutable('-1 Month');
Andrew Atkinson
  • 4,103
  • 5
  • 44
  • 48
3

If you are using PHP 5.3, it is because you need to set the default time zone:

date_default_timezone_set()
SeanJA
  • 10,234
  • 5
  • 32
  • 42
3

strtotime() is returning bool(false), because it can't parse the string '+one year' (it doesn't understand "one"). false is then being implicitly cast to the integer timestamp 0. It's a good idea to verify strtotime()'s output isn't bool(false) before you go shoving it in other functions.

From the docs:

Return Values

Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, this function would return -1 on failure.

Frank Farmer
  • 38,246
  • 12
  • 71
  • 89
  • Yeah, good point. My production code will have that, but I was tearing my hair out trying to get this to work, so stripped it down to as little code as possible. Thanks! – Matt Dec 15 '09 at 16:02
2

There is also a simpler and less sophisticated solution:

$monthDay = date('m/d');
$year = date('Y')+1;
$oneYearFuture = "".$monthDay."/".$year."";
echo"The date one year in the future is: ".$oneYearFuture."";
Daniel Lima
  • 798
  • 8
  • 13
2

Try This

$nextyear  = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($startDate)),   date("d",strtotime($startDate)),   date("Y",strtotime($startDate))+1));
Treby
  • 1,328
  • 6
  • 18
  • 26
2

You can use strtotime() to get future time.

//strtotime('+1 day');
//strtotime('+1 week');
//strtotime('+1 month');

 $now = date('Y-m-d'); 
 $oneYearLaterFromNow = date('Y-m-d', strtotime('+1 year'));
 $oneYearLaterFromAnyDate = date('Y-m-d', strtotime('+1 year', strtotime($anyValidDateString)));
infomasud
  • 2,263
  • 1
  • 18
  • 12
1

In my case (i want to add 3 years to current date) the solution was:

$future_date = date('Y-m-d', strtotime("now + 3 years"));

To Gardenee, Treby and Daniel Lima: what will happen with 29th February? Sometimes February has only 28 days :)

Tom Fuller
  • 5,291
  • 7
  • 33
  • 42
aiuen
  • 43
  • 3
1

My solution is: date('Y-m-d', time()-60*60*24*365);

You can make it more "readable" with defines:

define('ONE_SECOND', 1);
define('ONE_MINUTE', 60 * ONE_SECOND);
define('ONE_HOUR',   60 * ONE_MINUTE);
define('ONE_DAY',    24 * ONE_HOUR);
define('ONE_YEAR',  365 * ONE_DAY);

date('Y-m-d', time()-ONE_YEAR);