Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

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

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
derekstattin
Asked:
derekstattin
2 Solutions
 
sistemuCommented:
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
 
Ray PaseurCommented:
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now