Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to take the month difference of two dates in MySQL

Tags:

mysql

how to take the month difference of two dates in MySQL.

I m trying to get the month difference of two dates but I'm getting no. of days.

select datediff('2014-10-17T00:00:00.000-07:00', '2015-02-06T00:00:00.000-08:00'); 
like image 825
Ravi Ranjan Avatar asked Dec 18 '25 10:12

Ravi Ranjan


1 Answers

TIMESTAMPDIFF()

is your solution.


Syntax would be

TIMESTAMPDIFF(unit, datetime_expr1, datetime_expr2);

Returns datetime_expr2 − datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. One expression may be a date and the other a datetime; a date value is treated as a datetime having the time part '00:00:00' where necessary.

~MySQL :: MySQL 5.5 Reference Manual :: 12.7 Date and Time Functions~

Legal values for unit

  • MICROSECOND (microseconds)
  • SECOND
  • MINUTE
  • HOUR
  • DAY
  • WEEK
  • MONTH
  • QUARTER
  • YEAR

Examples

mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
        -> 3
mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');
        -> -1
mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');
        -> 128885
like image 199
Roshana Pitigala Avatar answered Dec 20 '25 01:12

Roshana Pitigala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!