Add six months in php

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.


I find working with DateTime much easier to use:

$datetime = new \DateTime();
$datetime->modify('+6 months');
echo $datetime->format('d');

or

$datetime = new \DateTime();
$datetime->add(new DateInterval('P6M'));
echo $datetime->format('d');

or in PHP version 5.4+

echo (new \DateTime())->add(new \DateInterval('P6M'))->format('d');

You can use the DateTime class in conjunction with the DateInterval class:

<?php
$date = new DateTime();
$date->add(new DateInterval('P6M'));

echo $date->format('d M Y');

If you still wanted to use strtotime and the date function as opposed to a DateTime() object, the following would work:

date('d', strtotime('+6 months'));

Tags:

Datetime

Php

Date