tags:

views:

263

answers:

5

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?

Thanks!

+3  A: 

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

K Prime
While that does return the right answer, the other one does not actually return an error either... just the wrong date.
SeanJA
strtotime doesn't throw errors. It returns false in the case of an error.
Frank Farmer
PHP will throw warnings if the default timezone is not set... though apparently that is not what he meant
SeanJA
+1  A: 

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

date_default_timezone_set()
SeanJA
+1  A: 

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
+2  A: 

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
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
+2  A: 

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

Nidhin Baby
Adding "+365" instead of "+1 year" did it. Thanks!
Matt
"* 365 days" rather.
Matt