Add 2 months to date php
I wrote this piece of code in order to display the current date + 2 months : Show
It does not seem to work as it displays : 01/03/1970. What am I doing wrong? Thanks for your help. EDIT : After reading comments and answers, I simplified and corrected it.
asked May 14, 2012 at 15:43
morgimorgi 9853 gold badges16 silver badges24 bronze badges 2 You're missing the second argument for the second
answered May 14, 2012 at 15:45
Alix AxelAlix Axel 148k91 gold badges390 silver badges493 bronze badges 1 Try using the DateTime object:
answered May 14, 2012 at 15:44
John CondeJohn Conde 215k98 gold badges447 silver badges489 bronze badges 4 If today is "YYYY-mm-31" and next month does not have the 31th day, it will show the next month of that day, make the system display "+3 months" result instead of "+2 months" result. So I guess this is the most safety:
Change the date to the 1st day first. answered Jul 28, 2016 at 3:48
Fenix LamFenix Lam 3665 silver badges19 bronze badges Using DateTime->add() or DateTime->modify() If you are working with an existing DateTime object, you can use one of these:
Beware of adding months in PHP, it may overflow to the next month if the day in the original date is higher than the total number of days in the new month. Same overflow happens with leap years when adding years. Somehow this is not considered a bug by PHP developers and is just documented without a solution. More here: PHP DateTime::modify adding and subtracting months I found this to be the most to-the-point solution to address overflow problem:
answered Mar 11, 2018 at 14:45
SlavaSlava 2,8072 gold badges29 silver badges38 bronze badges November 4, 2021 Category : PHP Today, i would like to show you how to add months to date in php. you will learn how to add 6 months to current date in php. if you have question about php add months to date then i will give simple example with solution. We will use php add 1 month to current date. I will give you very simple example how to add months to date in PHP. so let's see both example with output: Example 1: PHP Add Months to Date index.php
Output:
Example 2: PHP Add Months to Current Date index.php
Output:
i hope it can help you... Hardik SavaniI'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency. Follow Me: We are Recommending you
Popular Posts
Categories
Latest Posts
How to add months to date in php?Example 1: PHP Add Months to Date. index.php. $date = "2021-11-01"; $newDate = date('Y-m-d', strtotime($date. ' + 3 months')); ... . Output: 2022-02-01.. index.php. $newDate = date('Y-m-d', strtotime(' + 4 months')); echo $newDate; ?>. Output: Read Also: How to Add Days to Date in PHP? 2022-03-01. i hope it can help you.... How can I get plus date in PHP?Method 2: Using date_add() Function: The date_add() function is used to add days, months, years, hours, minutes and seconds. Syntax: date_add(object, interval);
Which function can be used to change the date from current month to three months previous?You can use the EDATE function to quickly add or subtract months from a date. The EDATE function requires two arguments: the start date and the number of months that you want to add or subtract. To subtract months, enter a negative number as the second argument. For example, =EDATE("9/15/19",-5) returns 4/15/19.
|