using php to find the rounded down number of months between two dates

Posted on 2008-11-17
Last Modified: 2013-12-13
I want to find the number of days between two dates, then find the fractional number of months between the two dates, then rounddown the number of frational months to a whole number. I know there are a number of ways to do this and was hoping you could give me some tips on the best way. I have reviewed some of the examples on and there are many ways that this can be done.
here is a brief e.g.
$start_date = 11/1/07
$final_date = 11/17/08
$final_date - $start_date = $number of days
$months = $number_of_days/365.25 *12
$months_rounded_down = rounddown($months)

function round_down($num, $d = 0)
    return sgn($num)*p_floor(abs($num), $d);

thanks for the help
Question by:derekstattin
    LVL 3

    Assisted Solution


    The number of days between two dates was already solved in this question:

    I am not aware of other, simpler methods, to calculate the number of months.
    LVL 107

    Accepted Solution

    You should probably carry your dates in ISO8601 format like YYYY-MM-DD.  This is the MySQL DATE format, and is created in PHP with date('Y-m-d').  This format has the advantages of being sortable and unambiguous.  You can make dates pretty for human-readable display, but internally you are better off using the ISO standard.

    Days between 2 dates:

    Tell us a little bit more about what you want to do with months.  For example, do you want to count the calendar months, not considering the differences in the number of days?  If so, you might use year difference * 12 + (old_month - new_month - 1).  If you want to count 30-day periods, you need a little more math.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now