Adding Three Months to a Date in PHP

Adding three months to a date in PHP

Change it to this will give you the expected format:

$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));

Add 3 month to the given date in a loop PHP

You can use PHP's dateTime class' modify() method to add +3 months in loop to it.

<?php
$dateStamp = "2015-06-01 00:00:00";
$date = new DateTime($dateStamp);
$datesArr = [];
for ($i=1; $i<21 ; $i++) {
$date->modify('+3 month');
$datesArr[] = $date->format('Y-m-d h:i:s');
}
echo '<pre>';
print_r($datesArr);
echo '</pre>';

Output:

Array
(
[0] => 2015-09-01 12:00:00
[1] => 2015-12-01 12:00:00
[2] => 2016-03-01 12:00:00
[3] => 2016-06-01 12:00:00
[4] => 2016-09-01 12:00:00
[5] => 2016-12-01 12:00:00
[6] => 2017-03-01 12:00:00
[7] => 2017-06-01 12:00:00
[8] => 2017-09-01 12:00:00
[9] => 2017-12-01 12:00:00
[10] => 2018-03-01 12:00:00
[11] => 2018-06-01 12:00:00
[12] => 2018-09-01 12:00:00
[13] => 2018-12-01 12:00:00
[14] => 2019-03-01 12:00:00
[15] => 2019-06-01 12:00:00
[16] => 2019-09-01 12:00:00
[17] => 2019-12-01 12:00:00
[18] => 2020-03-01 12:00:00
[19] => 2020-06-01 12:00:00
)

Working Code:

How to add months to a particular date

You have a Unix timestamp, not an actual date. Here I use the DateTime class to create a datetime object using that Unix timestamp. Then I can add a month to it and format the output.

$date = new DateTime('@'.$student->date);
$date->modify('+1 month');
echo $date-format('jS F Y');

If you want to stick to using date() and strtotime() you would use this:

echo date("jS F Y", strtotime("+1 month", $student->date));

strtotime() would take the starting date as the second parameter and then how you wish to modify it as your first parameter.

PHP add month to date on a date with day of month 31

I think this should work -

$time = strtotime("2015-05-31");
echo $final = date("Y-m-d", strtotime("first day of next month", $time));

PHP: Add months/days/years to user supplied date

The right way : DateTime::add -- date_add — Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
http://php.net/manual/en/datetime.add.php
Using native functions is always fastest, and in your case seems the proper way to go!

increment date by one month

$time = strtotime("2010.12.11");
$final = date("Y-m-d", strtotime("+1 month", $time));

// Finally you will have the date you're looking for.

Adding months to a date when there are 28,29 days in month

By default DateTime::modify('+1 month') will add the number of days of the current month to DateTime object.

You can fix this by adding the number of days of the upcoming month, like this:

<?php
function addMonth($date, $count)
{

$end = clone new DateTime($date);

$nextMonth = clone $end;
/*set the date to be first of month to get the next month*/
$nextMonth->modify('first day of this month');

for($i = 0;$i < $count; $i++){

/*get the number of days in the next month*/
$nextMonth->modify('next month')->modify('last day of this month');
$numOfDaysInNextMonth = $nextMonth->format('d');

/*add number of days in the next month to the end date*/
$end->modify("+$numOfDaysInNextMonth days");

$nextMonth->modify('first day of this month');
}
return $end->format('Y-m-d');
}

echo addMonth('2019-01-31', 1)."<br>"; // 2019-02-28
echo addMonth('2019-01-31', 2)."<br>"; // 2019-03-31
echo addMonth('2019-01-31', 3)."<br>"; // 2019-04-30

PHP: Adding months to a date, while not exceeding the last day of the month

You can compare the day of the month before and after you add 1 month. If it's not the same, you exceeded the next month.

function add($date_str, $months)
{
$date = new DateTime($date_str);

// We extract the day of the month as $start_day
$start_day = $date->format('j');

// We add 1 month to the given date
$date->modify("+{$months} month");

// We extract the day of the month again so we can compare
$end_day = $date->format('j');

if ($start_day != $end_day)
{
// The day of the month isn't the same anymore, so we correct the date
$date->modify('last day of last month');
}

return $date;
}

$result = add('2011-01-28', 1); // 2011-02-28
$result = add('2011-01-31', 3); // 2011-04-30
$result = add('2011-01-30', 13); // 2012-02-29
$result = add('2011-10-31', 1); // 2011-11-30
$result = add('2011-12-30', 1); // 2011-02-28

Current date + 2 months

You're missing the second argument for the second strtotime() call:

echo date('d/m/Y', strtotime('+2 months'));

PHP add 1 month to date

simple method:

$next_month = strtotime('august 2012 next month');

better method:

$d = new Date('August 2012');
$next_month = $d->add(new DateInterval('P1M'));

relevant docs: strtotime date dateinterval



Related Topics



Leave a reply



Submit