Mysql - aylar ve yıllar içinde alın

3 Cevap php

Mysql TARİH sütunu kullanarak, bir tarihten bu yana ay ve yıl # almak için herhangi bir yolu var mı?

Ben 2009/01/01 girerseniz .. ben 3 ay .. ve böylece, 1 yıl istiyorum.

3 Cevap

DateDiff iki tarih arasındaki gün farkı dönebilirsiniz. From there, you might have to do the math yourself. You could make a Stored Procedure with all the logic, but for the most part SQL isn't designed as a functional language.

EDIT: Senin için ne arıyor bulundu. Bu deneyin:

DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), <date>)), '%c months and %Y years')

Bir SQL sorgusu içinde, veya PHP ile?

http://www.phpbuilder.com/board/showthread.php?t=10362190

select mnth as months_between, mnth/12 as years_between
from
( select (year(<current date column>) - year(<older date column>))*12 +
         (month(<current date column>) - month(<older date column)) as mnth
  from <tables with date columns>
  where <conditions to get your rows with the dates you need to compare>
)

Ayrıca bkz. Tarih Aritmetik Anthony Mollinaro en SQL Cookbook, bölüm 8.