22 Votes

PHP: Add X Months to a Date

Question by Guest | Last update on 2021-06-13 | Created on 2016-07-23

I would like to calculate the date that is X months in future from an arbitrary given date in PHP.

For example, I want to determine the date one month or three months further. If the 23.7. is given, I want to get to the 23.8. or the 23.10. as a result. Of course, the function should consider a possible year change.

My main problem is, that each month has a different number of days, so that it is not sufficient to just add 30 days or so. Is there any function available for that purpose?

ReplyPositiveNegative
2Best Answer2 Votes

You can quite easily calculate that date using strtotime() and the following syntax:

$d = time();

echo date("d.m.Y", strtotime("+1 Months", $d));
echo date("d.m.Y", strtotime("+3 Months", $d));

In this example, first, we are determining the current date with time(). After that, we are adding either one month or three months to this date, so that we get the date into one or three months from today.

Instead of taking the current date, of course, we can also take any other arbitrary other initial date.
Last update on 2021-06-13 | Created on 2016-07-24

ReplyPositive Negative
Reply

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

PHP: Determine Week Number

Tutorial | 0 Comments

PHP: Date 3 Months ago

Question | 1 Answer

Change Date of File

Tutorial | 0 Comments

VirtualBox: Change Date and Time

Tutorial | 10 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.