$date + 1 year?

PhpStrtotime

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

$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!

Solution 2 - Php

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"));

Solution 3 - Php

//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 :)

Solution 4 - Php

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

Solution 5 - Php

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

<?php (date('Y')+1).date('-m-d'); ?>

Solution 6 - Php

// 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;

Solution 7 - Php

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');

Solution 8 - Php

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

date_default_timezone_set()

Solution 9 - Php

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.

Solution 10 - Php

Try This

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

Solution 11 - Php

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."";

Solution 12 - Php

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);

Solution 13 - Php

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 :)

Solution 14 - Php

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)));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMattView Question on Stackoverflow
Solution 1 - PhpMishoView Answer on Stackoverflow
Solution 2 - PhpNidhin BabyView Answer on Stackoverflow
Solution 3 - PhpDeveloperView Answer on Stackoverflow
Solution 4 - PhpK PrimeView Answer on Stackoverflow
Solution 5 - PhpGardeneeView Answer on Stackoverflow
Solution 6 - PhpAnarchyOutlawView Answer on Stackoverflow
Solution 7 - PhpAndrew AtkinsonView Answer on Stackoverflow
Solution 8 - PhpSeanJAView Answer on Stackoverflow
Solution 9 - PhpFrank FarmerView Answer on Stackoverflow
Solution 10 - PhpTrebyView Answer on Stackoverflow
Solution 11 - PhpDaniel LimaView Answer on Stackoverflow
Solution 12 - PhpAdam MesterView Answer on Stackoverflow
Solution 13 - PhpaiuenView Answer on Stackoverflow
Solution 14 - PhpinfomasudView Answer on Stackoverflow