Question: I'm looking to calculate the number of months between 2 date time fields.

Is there a better way than getting the Unix timestamp and then dividing by 2 592 000 (seconds) and rounding up within MySQL?

Answer: 
I use also [PERIOD\_DIFF](http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_period-diff). To get the year and the month of the date, I use the function [EXTRACT](http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_extract):

```
  SELECT PERIOD_DIFF(EXTRACT(YEAR_MONTH FROM NOW()), EXTRACT(YEAR_MONTH FROM time)) AS months FROM your_table;

```
From the MySQL manual:

> 
> PERIOD\_DIFF(P1,P2)
> 
> 
> Returns the number of months between periods P1 and P2. P1 and P2 should be in the format YYMM or YYYYMM. Note that the period arguments P1 and P2 are not date values.
> 
> 
> mysql> SELECT PERIOD\_DIFF(200802,200703);
>  -> 11
> 
> 
> 

So it may be possible to do something like this:

```
Select period_diff(concat(year(d1),if(month(d1)<10,'0',''),month(d1)), concat(year(d2),if(month(d2)<10,'0',''),month(d2))) as months from your_table;

```

Where d1 and d2 are the date expressions.

I had to use the if() statements to make sure that the months was a two digit number like 02 rather than 2.