Thursday, 12 June 2008

php strtotime returning 1970-01-29 - Adding month to date

Adding a month to a date
I was having problems using strtotime. The line i was using was

$next_available_date_end = strtotime("+2 months", next_available_date_start);

and the result I was getting was the date

I'm not sure why but opted to use mktime instead.
Here is how I accomplished this

$original_date = 2008-12-01;

$original_date_array = explode('-', $original_date ); // splits the date up

list($temp_year, $temp_month, $temp_day) = $original_date_array; // puts the array into the value year,month, day in sequence required

$temp_month++; // adds a month - we can do this because PHP will interperate 13 as the January in the next year

$date_a_month_on= date('Y-m-d', mktime(0, 0, 0, $temp_month, $temp_day, $temp_year));

for more on dates check

No comments: