Aaron Harker
Aaron Harker

Reputation: 323

strtotime giving odd result

OK I'm hoping someone can see where I'm going wrong.

$date = "2015-02-4";
$schedule = strtotime('+1 month',$date);

for some reason this is giving me 2680415 as the result instead of 1425488400 that I want but if I do

$date = "2015-02-4";
$schedule = strtotime($date);

I get the correct answer i.e. 1422982800.

$date is not really assigned like that, it is the result of a DB query added to the current month and year.

Upvotes: 0

Views: 109

Answers (2)

Stanislav Shabalin
Stanislav Shabalin

Reputation: 19278

As mentioned before, strtotime accepts int as a second parameter. So the string should be converted to timestamp before processing:

$date = "2015-02-4";
$schedule = strtotime('+1 month', strtotime($date));

Or use concatenation as shown in @mhall's answer.

Upvotes: 0

mhall
mhall

Reputation: 3701

You should append the +1 MONTH expression to your $date in the call to strtotime, instead of passing them as a separate parameters.

date_default_timezone_set('Asia/Bangkok');

$date     = "2015-02-4";
$schedule = strtotime($date);
echo "Original timestamp:  ", $schedule, PHP_EOL;
echo "Original date:       ", date("Y-m-d", $schedule), PHP_EOL;

$schedule = strtotime($date . ' +1 MONTH');
echo "+ 1 month timestamp: ", $schedule, PHP_EOL;
echo "+ 1 month date:      ", date("Y-m-d", $schedule), PHP_EOL;

Output:

Original timestamp:  1422982800
Original date:       2015-02-04
+ 1 month timestamp: 1425402000
+ 1 month date:      2015-03-04

Upvotes: 2

Related Questions