Solved

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

Posted on 2008-11-17
339 Views
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 php.net 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)

from php.net
function round_down(\$num, \$d = 0)
{
return sgn(\$num)*p_floor(abs(\$num), \$d);
}

thanks for the help
0
Question by:derekstattin

LVL 3

Assisted Solution

Hi,

The number of days between two dates was already solved in this question:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23780439.html

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

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:
http://us.php.net/manual/en/function.mktime.php#86916

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.
0

## Featured Post

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…