问题描述
我在mysql表中有以下三个日期范围,
I have three date ranges in mysql table as follow
- 从2013-09-29到2013-10-02
- 从2013-10-14到2013-10-16
- 从2013-10-28到2013-11-05
我只想计算十月份的某天,例如从第一个范围(2013-09-29到2013-10-02)相差两天(10月1日和10月2日),它应该忽略从9月一个月开始的天数,最后我想从上述日期范围算出给定月份的总天数。
I want to count only days that occur in Month of October, for example from first range (2013-09-29 to 2013-10-02) I should get difference of two days (1st and 2nd October) , and it should ignore days from September month, Finally i want to count total days in a given month from above date ranges.
可以通过直接的mysql查询来完成。或任何简短的PHP逻辑。
Can it be done from direct mysql query. or any short PHP logic.
这是我的表结构
id,employee_id,leave_start,leave_to
here is my table structureid,employee_id,leave_start,leave_to
我正在寻找类似
function countLeaves($ employee_id,$ month)
{
function countLeaves($employee_id,$month){
//代码
返回$ numberOfLeaves
}
return $numberOfLeaves}
推荐答案
您需要计算2013-10-01月的第一天和2013-10-31月的最后一天,然后可以使用如下查询:
You need to calculate the first day of the month 2013-10-01 and the last day of the month 2013-10-31 and then you could use a query like this:
SELECT
DATEDIFF(
LEAST(d_end, '2013-10-31'),
GREATEST(d_start, '2013-10-01'))+1 days
FROM
ranges
WHERE
d_start<='2013-10-31' AND d_end>='2013-10-01'
请参见小提琴。
这篇关于计算日期范围内一个月内的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!