-4

i have some problem with strtotime , when i try to addimg 2 month to my current date

Example :

'2014-12-16 13' => date('Y-m-d H');

$compare_date = date('Y-m-d H:i',strtotime('2014-12-16 13'.'+2 month'));
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38
Yra Yra
  • 1
  • 3

4 Answers4

2

I tested it in phpFiddle

It works if you specify minutes (Based on this code)

$compare_date = date('Y-m-d H:i',strtotime('2014-12-16 13'.'+2 month'));

You said H:i

so if you use:

$compare_date = date('Y-m-d H:i',strtotime('2014-12-16 13:00'.'+2 month'));//Note the :00

it works for me.

EDIT1:

Without H:i

$compare_date = date('Y-m-d',strtotime('2014-12-16'.'+2 month'));

EDIT2:

o-W

this works a little for me:

$compare_date = date('o-W',strtotime('2014-12-16'.'+2 month'));

EDIT3:

As comment says, get rid of dot

$compare_date = date('o-W',strtotime('2014-12-16 +2 month'));
Marco Mura
  • 582
  • 2
  • 13
1

You're using strtotime wrong. When using relative time (+2 month) you should provide a timestamp as second parameter (current timestamp is the default value).

$compare_date = date('Y-m-d H:i',strtotime('+2 month', strtotime('2014-12-16 13:00')));
Waldson Patricio
  • 1,489
  • 13
  • 17
0

Have you tried

$compare_date = date('Y-m-d H',strtotime('2014-12-16 13'.'+2 month'));

Instead of:

$compare_date = date('Y-m-d H:i',strtotime('2014-12-16 13'.'+2 month'));
Peter
  • 8,776
  • 6
  • 62
  • 95
0

Beware with using +month notice the behavior below if the month after has less days

echo date( "Y-m-d", strtotime( "2009-01-31 +1 month" ) ); // PHP:  2009-03-03
echo date( "Y-m-d", strtotime( "2009-01-31 +2 month" ) ); // PHP:  2009-03-31
Jelle Keizer
  • 723
  • 5
  • 9