Simon Fabb
Simon Fabb

Reputation: 53

PHP date from today to 4 months

Does anyone know why this keeps on showing the date 01-05-70?

$effectiveDate = strtotime("+4 months", strtotime($effectiveDate)); // returns timestamp
echo date('d-m-y',$effectiveDate); // formatted version

I want it to print today's date + 4 months.

Upvotes: 1

Views: 1113

Answers (3)

Pedro Lobito
Pedro Lobito

Reputation: 98961

Does anyone know why this keeps on showing the date 01-05-70?

$effectiveDate may contain an invalid timestamp, I prefer using the DateTime class, i.e.:

$d1 = DateTime::createFromFormat('d-m-Y', '09-08-2016');
$d1->add(new DateInterval('P4M'));
echo $d1->format('d-m-Y');

Demo

Upvotes: 0

Saurabh
Saurabh

Reputation: 784

$today = date('d-m-y');
$monthsFour = date('d-m-y',strtotime("+4 months"));
echo $today;
echo "<br>";
echo $monthsFour;

Your question in comment : Do you know how I express the year as 2016 as opposed to 16? Replace y in date function with capital Y

Edited:

$today = date('d-m-Y');
$monthsFour = date('d-m-Y',strtotime("+4 months"));
echo $today;
echo "<br>";
echo $monthsFour;

Upvotes: 0

John Conde
John Conde

Reputation: 219874

In your code $effectiveDate contains an invalid date. So strtotime() returns the unix epoch Jan 1, 1970.

But you shouldn't need that variable at all if all you want is the date four months from now.

echo date('d-m-y',strtotime('+4 months'));

Upvotes: 2

Related Questions