How to find first day of the next month and remaining days till this date with PHP

Create a timestamp for 00:00 on the first day of next month:

$firstDayNextMonth = strtotime('first day of next month');

The number of days til that date is the number of seconds between now and then divided by (24 * 60 * 60).

$daysTilNextMonth = ($firstDayNextMonth - time()) / (24 * 3600);

$firstDayNextMonth = date('Y-m-d', strtotime('first day of next month'));

For getting first day after two months from current

$firstDayAfterTwoMonths = date('Y-m-d', strtotime('first day of +2 month'));

You can use DateTime object like this to find out the first day of next month like this:

$date = new DateTime('first day of next month');

You can do this to know how many days left from now to the first day of next month:

$date = new DateTime('now');
$nowTimestamp = $date->getTimestamp();
$date->modify('first day of next month');
$firstDayOfNextMonthTimestamp = $date->getTimestamp();
echo ($firstDayOfNextMonthTimestamp - $nowTimestamp) / 86400;

Tags:

Php