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
96.2k82 gold badges294 silver badges428 bronze badges
asked Dec 15, 2009 at 3:48
4
$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!
answered Jan 25, 2013 at 15:17
MishoMisho
2,8132 gold badges13 silver badges9 bronze badges
1
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 timestampTry 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
96.2k82 gold badges294 silver badges428 bronze badges
answered Dec 15, 2009 at 9:09
Nidhin BabyNidhin Baby
1,5784 gold badges13 silver badges16 bronze badges
9
//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 :)
answered Jul 31, 2017 at 15:26
DeveloperDeveloper
3,6774 gold badges36 silver badges45 bronze badges
Try: $futureDate=date('Y-m-d',strtotime('+1 year',$startDate));
answered Dec 15, 2009 at 3:52
K PrimeK Prime
5,7491 gold badge24 silver badges19 bronze badges
4
just had the same problem, however this was the simplest solution:
<?php (date('Y')+1).date('-m-d'); ?>
answered Mar 29, 2013 at 9:18
GardeneeGardenee
1152 silver badges7 bronze badges
1
// 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;
answered Jan 6, 2016 at 23:59
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: //php.net/manual/en/class.datetimeimmutable.php
If you just want from todays date then you can always do:
new \DateTimeImmutable('-1 Month');answered Dec 14, 2016 at 17:06
Andrew AtkinsonAndrew Atkinson
3,9735 gold badges41 silver badges48 bronze badges
If you are using PHP 5.3, it is because you need to set the default time zone:
date_default_timezone_set()answered Dec 15, 2009 at 3:55
SeanJASeanJA
9,9985 gold badges31 silver badges42 bronze badges
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.
answered Dec 15, 2009 at 4:00
Frank FarmerFrank Farmer
37.2k12 gold badges70 silver badges89 bronze badges
1
Try This
$nextyear = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($startDate)), date("d",strtotime($startDate)), date("Y",strtotime($startDate))+1));answered Dec 15, 2009 at 3:56
TrebyTreby
1,3206 gold badges18 silver badges26 bronze badges
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."";answered Jun 16, 2013 at 4:59
Daniel LimaDaniel Lima
7888 silver badges13 bronze badges
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);answered May 22, 2020 at 13:41
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)));answered Oct 28, 2021 at 11:05
infomasudinfomasud
1,24311 silver badges12 bronze badges
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,0616 gold badges32 silver badges42 bronze badges
answered Nov 4, 2014 at 13:48
aiuenaiuen
332 bronze badges