user3139875
user3139875

Reputation: 3

php strtotime giving wrong result

This line of PHP used to add 18 months to the date 27/12/2013 but it doesn't seem to work any more:

echo date( "d/m/Y", strtotime( "27/12/2013 +18 month") );exit;

It now returns 01/01/1970

I would appreciate any thoughts on this.

Upvotes: 0

Views: 491

Answers (5)

Awlad Liton
Awlad Liton

Reputation: 9351

Test here: https://eval.in/83396

echo date( 'd/m/Y', strtotime( "+18 months", strtotime("12/27/2013 ")) );

Upvotes: 0

Mark Reed
Mark Reed

Reputation: 95252

From the strtotime manual page:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

So, "27/12/2013" will never work in an argument to strtotime. You can use "27-12-2013" or "27.12.2013" or "2013-12-27" or "12/27/2013"... take your pick. I would, as recommended above, stick with ISO 8601 ("2013-12-27").

Upvotes: 0

Jessica
Jessica

Reputation: 7005

27/12/2013 is not a valid format for PHP. Here are a list of valid formats. http://www.php.net/manual/en/datetime.formats.php

http://www.php.net/manual/en/datetime.formats.date.php

You'll see, DD/MM/YYYY is not an accepted format.

echo date( "d/m/Y", strtotime( "2013-12-27 +18 months") );

Output: 27/06/2015

Upvotes: 1

Peon
Peon

Reputation: 8020

Since d/m/Y is not a standart type ( the standart would be m/d/Y, Y-m-d or d.m.Y ), you need to reformat it and then add the time. I suggest you to use DateTime for this:

$d = DateTime::createFromFormat( 'd/m/Y', '27/12/2013' );
$d->modify( '+18 month' );
echo $d->format( 'd/m/Y' );

Upvotes: 3

Goikiu
Goikiu

Reputation: 560

you need to revert the date maybe echo date( "d/m/Y", strtotime( "2013/12/27 +18 month") );exit;

Upvotes: 0

Related Questions