SpiderT3 Posted January 30, 2009 Share Posted January 30, 2009 I've been trying to figure out how to subtract two dates to get the number of months between the two dates. It was frustrating at first because I was using a timestamp, and subtracting the two dates to figure out the seconds in between the two dates. But since each month has different amount of days in each of them, each month is not the same amount of seconds either. I finally figured it out, and its pretty simple. Check it out. I basically am working with the format of "YYYY-MM-DD" and am using it to calculate if a trial period has been used up in a subscription website I'm building. So I take todays date and the person's signup date, and calculate the difference in months by multiplying the year by twelve and add it to the number month. subtract the two and there we go! It's so nice when its that simple, reminds me of high school when you were doing fractions, you just have to find the "common denominator"! Because I'm using a cron job to execute this on the same day each month, I don't need to worry about calculating days. $subscription_start = strtotime($subscr_change); $now = strtotime($today); function total_months($a){ $year = date("Y", $a); $ymonths= $year * 12; $month = date("n", $a); $total = $ymonths + $month; return $total; } $dif = abs(total_months($subscription_start) - total_months($today)); Quote Link to comment Share on other sites More sharing options...
rubing Posted January 30, 2009 Share Posted January 30, 2009 Is there a question? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.