With the change of strategy, I did the editing of SQL so that it calculates like this:
-
If the months are different it does two calculation of the month of the entry and the month of the exit.
-
If the months have differences of more months type month 05 to month 07 you would then get the middle and make a calculation in PHP code to find out type the month 6 has 30 days the same is in SQL 1, 3 , 5, 7, 8, 10, 12 have 31 days, month 2 depending on the year can have 28 or 29 days (take ano % 2 == 0
if true this expression has 29 days if not 28 days), and the others months has 30 days.
-
If the entered month belongs to the entry, enter the DIFERENCA_MENTRADA
field if the month entered belongs to the month of the output and get DIFERENCA_MSAIDA
.
SELECT IF(mentrada <> msaida,
(
CASE mentrada
WHEN (1 OR 3 OR 5 OR 7 OR 8 OR 10 OR 12) THEN
DATEDIFF(date_format(concat(yentrada,'-', mentrada,'-', 31), '%Y-%m-%d'), data_entrada)
WHEN (2) THEN
DATEDIFF(date_format(concat(yentrada,'-', mentrada,'-', IF(yentrada % 4 = 0,29,28)), '%Y-%m-%d'), data_entrada)
ELSE
DATEDIFF(date_format(concat(yentrada,'-', mentrada,'-', 30), '%Y-%m-%d'), data_entrada)
END
), DATEDIFF(data_saida, data_entrada)) as diferenca_mentrada,
IF (mentrada <> msaida,
DATEDIFF(data_saida, date_add(date_format(concat(ysaida,'-', msaida,'-1'), '%Y-%m-%d'), INTERVAL -1 DAY)),0) as diferenca_msaida,
id_funcionario,
data_entrada,
data_saida,
mentrada,yentrada,
msaida,ysaida
FROM (
SELECT
id_funcionario,
data_entrada,
data_saida,
month(data_entrada) mentrada,
year(data_entrada) yentrada,
month(data_saida) msaida,
year(data_saida) ysaida
FROM historico
) as historico WHERE (mentrada = 6 or msaida = 6) and yentrada = 2014 AND id_funcionario = 1
SQLFiddle SQLFiddle
Month 5: Then take the difference
Month6:Thengetthedifference_msaida