sm21guy
sm21guy

Reputation: 626

php date plus 1 year outputting weird date

I want to add one year to $joindate for the value of $exdate.

The php code is as shown below:

date_default_timezone_set('UTC');
$joindate = date("d/m/Y");
$exdate = date("d/m/Y",
    strtotime(date("d/m/Y", strtotime($joindate)) . " + 365 day"));

However, when I echo out both variables I get this:

Join date : 16/12/2011
Ex Date : 01/01/1971

(the ex date should be 16/12/2012)

Anyone knows where is the mistake I made?

Thanks.

Upvotes: 3

Views: 6531

Answers (7)

Yousuf Ginwalla
Yousuf Ginwalla

Reputation: 11

the most easiest way of adding a year to current year is this intval(date('Y'))+1.

Upvotes: 1

Alex Pliutau
Alex Pliutau

Reputation: 21957

$joindate = date('d/m/Y');
$exdate   = date('d/m/Y', strtotime('+1 year'));

Upvotes: 8

ArVan
ArVan

Reputation: 4275

Try this:

 $date = strtotime("+365 day", strtotime("2011-12-16"));
 echo date("Y-m-d", $date);

Upvotes: 3

Chandresh M
Chandresh M

Reputation: 3828

try this line of code.

$dateOneYearAdded = strtotime(date("Y-m-d", strtotime($currentDate)) . " +1 year");
echo "Date After one year: ".date('l dS \o\f F Y', $dateOneYearAdded)."<br>";

Thanks.

Upvotes: 0

Arfeen
Arfeen

Reputation: 2623

Your code can also work if you change format to "m/d/Y"

Upvotes: 0

L&#233;on Rodenburg
L&#233;on Rodenburg

Reputation: 4894

Have a look at mktime() in the PHP manual (here)

With it you can add days, months, years, hours, minutes and seconds to an existing date like this:

$date = "2011-12-16";
$dateTime = strtotime($date);

$day = date("d", $dateTime);
$month = date("m", $dateTime);
$year = date("Y", $dateTime) + 1;
$hour = date("H", $dateTime);
$minute = date("i", $dateTime);
$second = date("s", $dateTime);

$nextYearTime = mktime($hour, $minute, $second, $month, $day, $year);  

$nextDate = date("Y-m-d", $nextYearTime);

You can also wrap this code (or variations on it) into a function for portability.

Upvotes: 1

xdazz
xdazz

Reputation: 160833

$exdate = date("d/m/Y", strtotime("+ 365 day"));

And the $exdate will be 15/12/2012 because 2012 is leap year which has 366 days.

Upvotes: 4

Related Questions