Add six months in php

后端 未结 4 821
难免孤独
难免孤独 2020-12-03 21:46

I\'m trying to get the month, six months out from the current date.

I\'ve tried using:

date(\'d\', strtotime(\'+6 month\', time()));

B

4条回答
  •  星月不相逢
    2020-12-03 22:28

    You don't need to pass time() to strtotime, as it is the default.

    Apart from that, your approach is correct - except that you take date('d') (which is putting out the day) and not date('m') for the month, so echo date('m', strtotime('+6 month')); should do.

    Nevertheless, I would recommend using the DateTime way, which John stated. DateTime has several advantages over the "old" date functions, for example they don't stop working when the seconds since the UNIX big bang don't fit into an 32bit integer any more.

提交回复
热议问题