Jump to content

How many months between two dates


SpiderT3

Recommended Posts

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));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.