PHP |计算日期范围内当月的天数
PHP | count num of days for current month in a date range
我正在尝试从日期范围中获取当前月份或任何其他月份的日期。
假设我的日期范围如下。
$startDate = "2014-12-10";
$endDate = "2015-2-3";
我只想计算当月 days/dates "February"
如果开始日期和结束日期大于 1,则结果为 3。
但是我怎样才能让它以编程方式工作呢??
-=-=-=-=-==-=
更新:
我想我无法解释我的问题,
让我们采用相同的日期范围..
如果我想以编程方式取出 12 月份的天数
就像
21 天,因为开始日期是 2014-12-10
;
日期在以编程方式来自数据库的范围内..
-=-==-=-=-=-=-=
更新 2:
另一个简单的例子
让我们假设 28-1-2015
到 6-2-2015
的员工是否已获准休假
所以这里的员工休假开始日期为
$sartDate = '28-1-2015';
$endDate = '6-2-2015';
所以员工休假总数是
$totalleaves = $endDate - $startDate //It is not right way to take out the differece only For sake of Example shown it
这将使我的总假期 9
或 10
天
但如果我们看到,这些叶子会分成两个不同的月份。
我想生成一份报告,我想看看员工在特定月份休了多少假,假设是上个月 January
我认为以下日期是 4 天,因为以下日期属于日期范围并且属于 1 月。
28-1-2015
29-1-2015
30-1-2015
31-1-2015
所以如果我想得到每个月离开的数组的结果,它会
像
array(
'January' => array(
'TotalLeavesTaken' => 4
),
'February' => array(
'TotalLeavesTaken' => 6
)
);
我认为这是我能解释的最好的了..
不确定是否正确理解了你的问题,
date('d', strtotime($endDate));
Lets try this
$month = strtotime(date('Y-m-01', time()));
$daysCount = (int)(time() - $month) / (24 * 3600);
它会对你有所帮助:我正在用它来度过几天
$date_diff = $end_date - $start_date;
//difference of two dates
这将 return 你 days.Is 只是你想要这个或其他东西 else.Please 确认它是否对你有帮助。
$days_left = floor($date_diff / (60*60*24));// No. of days
问题更新后调整
好的,根据你上次的更新,我已经调整好了。希望这就是您要找的:
function getLeavesInPeriod($start, $end) {
$date = new DateTime($start);
$endDate = new DateTime($end);
$leaves = array();
while($date <= $endDate ) {
$year = $date->format('Y');
$month = $date->format('M');
if(!array_key_exists($year, $leaves))
$leaves[$year] = array();
if(!array_key_exists($month, $leaves[$year]))
$leaves[$year][$month] = 0;
$leaves[$year][$month]++;
$date->modify("+1 day");
}
return $leaves;
}
$leaves = getLeavesInPeriod("2015-1-5", "2015-2-3");
print $leaves[2015]["Jan"]; //27
我正在尝试从日期范围中获取当前月份或任何其他月份的日期。
假设我的日期范围如下。
$startDate = "2014-12-10";
$endDate = "2015-2-3";
我只想计算当月 days/dates "February"
如果开始日期和结束日期大于 1,则结果为 3。
但是我怎样才能让它以编程方式工作呢??
-=-=-=-=-==-=
更新:
我想我无法解释我的问题,
让我们采用相同的日期范围..
如果我想以编程方式取出 12 月份的天数
就像
21 天,因为开始日期是 2014-12-10
;
日期在以编程方式来自数据库的范围内..
-=-==-=-=-=-=-=
更新 2:
另一个简单的例子
让我们假设 28-1-2015
到 6-2-2015
所以这里的员工休假开始日期为
$sartDate = '28-1-2015';
$endDate = '6-2-2015';
所以员工休假总数是
$totalleaves = $endDate - $startDate //It is not right way to take out the differece only For sake of Example shown it
这将使我的总假期 9
或 10
天
但如果我们看到,这些叶子会分成两个不同的月份。
我想生成一份报告,我想看看员工在特定月份休了多少假,假设是上个月 January
我认为以下日期是 4 天,因为以下日期属于日期范围并且属于 1 月。
28-1-2015
29-1-2015
30-1-2015
31-1-2015
所以如果我想得到每个月离开的数组的结果,它会 像
array(
'January' => array(
'TotalLeavesTaken' => 4
),
'February' => array(
'TotalLeavesTaken' => 6
)
);
我认为这是我能解释的最好的了..
不确定是否正确理解了你的问题,
date('d', strtotime($endDate));
Lets try this
$month = strtotime(date('Y-m-01', time()));
$daysCount = (int)(time() - $month) / (24 * 3600);
它会对你有所帮助:我正在用它来度过几天
$date_diff = $end_date - $start_date;
//difference of two dates
这将 return 你 days.Is 只是你想要这个或其他东西 else.Please 确认它是否对你有帮助。
$days_left = floor($date_diff / (60*60*24));// No. of days
问题更新后调整
好的,根据你上次的更新,我已经调整好了。希望这就是您要找的:
function getLeavesInPeriod($start, $end) {
$date = new DateTime($start);
$endDate = new DateTime($end);
$leaves = array();
while($date <= $endDate ) {
$year = $date->format('Y');
$month = $date->format('M');
if(!array_key_exists($year, $leaves))
$leaves[$year] = array();
if(!array_key_exists($month, $leaves[$year]))
$leaves[$year][$month] = 0;
$leaves[$year][$month]++;
$date->modify("+1 day");
}
return $leaves;
}
$leaves = getLeavesInPeriod("2015-1-5", "2015-2-3");
print $leaves[2015]["Jan"]; //27