Php 从日期范围开始计算一个月内的天数
我在mysql表中有三个日期范围,如下所示Php 从日期范围开始计算一个月内的天数,php,mysql,date,Php,Mysql,Date,我在mysql表中有三个日期范围,如下所示 从2013年9月29日到2013年10月02日 2013年10月14日至2013年10月16日 2013年10月28日至2013年11月05日 我只想计算10月份的天数,例如从第一个范围(2013-09-29到2013-10-02)我应该得到两天的差值(10月1日和2日),它应该忽略9月份的天数,最后我想从上述日期范围计算给定月份的总天数 可以通过直接mysql查询完成。或任何简短的PHP逻辑 这是我的桌子结构 id,员工id,离开开始,离开到 我
- 从2013年9月29日到2013年10月02日
- 2013年10月14日至2013年10月16日
- 2013年10月28日至2013年11月05日
/**
* @var $dateFrom string Date to count from,
* you can use date('Y-m-01') as start of current month
* @var $dateTo string End of period date, example: '2013-09-05'
**/
function getDaysCount($dateFrom, $dateTo)
{
return (int)(strtotime($dateTo) - strtotime($dateFrom)) / (24 * 3600);
}
您需要计算2013-10-01月的第一天和2013-10-31月的最后一天,然后可以使用如下查询:
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'
选择
DATEDIFF(
至少(d_end,'2013-10-31'),
最大(d_开始,'2013-10-01'))+1天
从…起
范围
哪里
d_start='2013-10-01'
请参阅fiddle。函数getAllDates($fromDate、$toDate、$rejectmonth)
{
如果(!$fromDate |!$toDate){返回false;}
$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);
for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24))
{
if($rejectmonth == date("m",$currentDateTS))
continue;
$currentDateStr = date("Y-m-d",$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
}
return $dateMonthYearArr;
}
$datemonthyearrarr=array();
$fromDateTS=STROTIME($fromDate);
$toDateTS=实时时间($toDate);
对于($currentDateTS=$fromDateTS;$currentDateTS告诉我您从数据库中获得的确切值…尝试使用gregoriantojd
并减去实际上我想计算一个月内员工的假期数,因此我的方法将如下所示code
函数countLeaves($employeeId,$month){//mysql查询+php逻辑返回$numberOfDays;}code
$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);
for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24))
{
if($rejectmonth == date("m",$currentDateTS))
continue;
$currentDateStr = date("Y-m-d",$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
}
return $dateMonthYearArr;
}