Matt
Matt

Reputation: 7212

$date + 1 year?

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?

Upvotes: 120

Views: 288441

Answers (15)

Gardenee
Gardenee

Reputation: 125

I just had the same problem, however calling date() twice and incrementing the year seems simplest solution to me.

echo (date('Y') + 1) . date('-m-d');

Upvotes: 8

microtekblue
microtekblue

Reputation: 29

I wanted 1 year range only for an HTML5 Date input. This is what worked for me:

   <input type="date"

           min="<?php echo date("Y-m-d"); ?>"

           max="<?php echo date((intval(date("Y"))+1)."-m-d"); ?>"
    />

1 Year from now:

date((intval(date("Y"))+1)."-m-d");

Upvotes: -1

Md Masud
Md Masud

Reputation: 2713

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

Upvotes: 3

Adam Mester
Adam Mester

Reputation: 81

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

Upvotes: 1

Developer
Developer

Reputation: 3998

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

Upvotes: 15

Andrew Atkinson
Andrew Atkinson

Reputation: 4251

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

Upvotes: 8

aiuen
aiuen

Reputation: 43

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

Upvotes: 1

AnarchyOutlaw
AnarchyOutlaw

Reputation: 153

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

Upvotes: 7

Daniel Lima
Daniel Lima

Reputation: 798

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

Upvotes: 2

Misho
Misho

Reputation: 3021

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

Upvotes: 264

Nidhin Baby
Nidhin Baby

Reputation: 1648

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

Upvotes: 114

Frank Farmer
Frank Farmer

Reputation: 39356

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.

Upvotes: 3

Treby
Treby

Reputation: 1320

Try This

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

Upvotes: 2

SeanJA
SeanJA

Reputation: 10354

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

date_default_timezone_set()

Upvotes: 3

K Prime
K Prime

Reputation: 5849

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

Upvotes: 9

Related Questions