Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7019109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:08:26+00:00 2026-05-27T23:08:26+00:00

I need a method for adding some number of months to any date in

  • 0

I need a method for adding some number of months to any date in PHP. I know how to do this in MySQL but not in PHP. Here’s my attempt:

MySQL:

    SELECT DATE_ADD( '2011-12-29', INTERVAL 2
    MONTH ) // Output "2012-02-29"

    SELECT DATE_ADD( '2011-12-30', INTERVAL 2
    MONTH )  // output "2012-02-29"

    SELECT DATE_ADD( '2011-12-31', INTERVAL 2
    MONTH )  // output "2012-02-29"

PHP:

    $date = date_create('2011-12-29');
    $date->modify("+1 month");
    echo $date->format("Y-m-d");
    // Output is "2012-01-29" -- this is correct 

    $date = date_create('2011-12-30');
    $date->modify("+2 month");
    echo $date->format("Y-m-d");
    // Output is "2012-03-01" -- I need the answer like "2012-02-29" 

    $date = date_create('2011-12-31');
    $date->modify("+2 month");
    echo $date->format("Y-m-d");
    // Output is "2012-03-02" -- I need the answer like "2012-02-29" 

The MySQL output is correct. I need the same output in PHP.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:08:27+00:00Added an answer on May 27, 2026 at 11:08 pm

    Here’s a solution that might do the job for you:

    function addMonths(DateTime $date, $months) {
        $last = clone $date;
        $last = $last->modify("last day of +$months months")->getTimestamp();
    
        $default = clone $date;
        $default = $default->modify("+$months months")->getTimestamp();
    
        return $date->setTimestamp(min($last, $default));
    }
    
    $date = new DateTime('2011-12-31');
    $laterDate = addMonths($date, 2);
    

    This will work regardless of which day of the month you start with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a method for adding business days in PHP. For example, Friday 12/5
I know downcasting like this won't work. I need a method that WILL work.
I need a method to return a random string in the format: Letter Number
I need a method in JavaScript to escape all characters which are not (
I already created a wordpress widget for adding some featured from admin panel.I need
I need to transform a string into a number using some oracle built-in methods
Well, the conversion with as.double appears to work, but then adding a non-integral number
In adding some js grids to an existing PHP application I ran into the
So i need some support with my Ruby assignment, I'm not from US so
So I am adding StaticTextField controls to a page. This is using ExtJS, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.