我想计算一下工作天数。
通过使用碳,我可以计算天数并减少周末。
$num_days = $to_date->diffInWeekdays($from_date) + 1;
我有一系列假期,如果在两天之间有假期,我想减少天数。
有没有办法做到这一点。
谢谢
答案 0 :(得分:3)
这是一个用于计算工作日的小型效用函数:
function getWorkingDaysCount($from, $to) {
$workingDays = [1, 2, 3, 4, 5]; // Working days (week days)
$holidayDays = ['*-12-25', '*-01-01']; // Holidays array, add desired dates to this array
$from = new DateTime($from);
$to = new DateTime($to);
$to->modify('+1 day');
$interval = new DateInterval('P1D');
$periods = new DatePeriod($from, $interval, $to);
$days = 0;
foreach ($periods as $period) {
if (!in_array($period->format('N'), $workingDays)) continue;
if (in_array($period->format('Y-m-d'), $holidayDays)) continue;
if (in_array($period->format('*-m-d'), $holidayDays)) continue;
$days++;
}
return $days;
}
您可以传递开始日期和结束日期以获取天数。
答案 1 :(得分:2)
您可以使用diffInDaysFiltered
来实现您的目标。
假设您的假期是一组Carbon
个实例,您可以执行以下操作:
$start = Carbon::now()->setDate(2014, 1, 1);
$end = Carbon::now()->setDate(2015, 1, 1);
$holidays = [
Carbon::create(2014, 2, 2),
Carbon::create(2014, 4, 17),
Carbon::create(2014, 5, 19),
Carbon::create(2014, 7, 3),
];
$days = $start->diffInDaysFiltered(function (Carbon $date) use ($holidays) {
return $date->isWeekday() && !in_array($date, $holidays);
}, $end);
如果它只是一个字符串数组,那么你可以做类似的事情:
!in_array($date->format('[the-format-of-your-dates]'), $holidays)
希望这有帮助!
答案 2 :(得分:0)
您可以通过以下代码获取此信息
<?php
function workdays($start,$end,$holidays){
$end = strtotime($end);
$start = strtotime($start);
$days = ($end - $start) / 86400 + 1;
$no_full_weeks = floor($days / 7);
$no_remaining_days = fmod($days, 7);
$the_first_day_of_week = date("N", $start);
$the_last_day_of_week = date("N", $end);
if ($the_first_day_of_week <= $the_last_day_of_week) {
if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;
if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;
}
else {
if ($the_first_day_of_week == 7) {
$no_remaining_days--;
if ($the_last_day_of_week == 6) {
$no_remaining_days--;
}
}
else {
$no_remaining_days -= 2;
}
}
$workingDays = $no_full_weeks * 5;
if ($no_remaining_days > 0 )
{
$workingDays += $no_remaining_days;
}
foreach($holidays as $holiday){
$time_stamp=strtotime($holiday);
if ($start <= $time_stamp && $time_stamp <= $end && date("N",$time_stamp) != 6 && date("N",$time_stamp) != 7)
$workingDays--;
}
return $workingDays;
}
echo workdays("2016-01-04","2016-01-30",array("2016-01-25","2016-01-26","2016-01-27"))
?>
答案 3 :(得分:0)
function getWorkingDays($days1,$start_dt,$end_dt,$holidays) {
$date1 = $start_dt;
$date2 = $end_dt;
$array = array();
$query = $holidays;// pass array of holidays.
foreach($query as $row)
{
$array[] = $row['h_date'];
}
$workingDays = [1, 2, 3, 4, 5];// 1 = Mon,...
$holidayDays = $array; # variable and fixed holidays
$from = new DateTime($date1);
$to = new DateTime($date2);
$to->modify('+1 day');
$interval = new DateInterval('P1D');
$periods = new DatePeriod($from, $interval, $to);
$nameOfDay = date('N', strtotime($date1));
if($nameOfDay==7)
{
$nameOfDay=0;
}
$days = $nameOfDay;
$fullDate=explode('-',$date1);
$y=$fullDate['0'];
$m=$fullDate['1'];
$d=$fullDate['2'];
foreach ($periods as $period) {
if (!in_array($period->format('N'), $workingDays)) {
$d++;
continue;
}
if (in_array($period->format('Y-m-d'), $holidayDays)) {
if($d==32 && $m==12)
{
$d=1;
$m=1;
$y++;
}
$d++;
continue;
}
if (in_array($period->format('*-m-d'), $holidayDays)) {
$d++;
continue;
}
switch ($m) {
case '2':
if( (0 == $y % 4) and (0 != $y % 100) or (0 == $y % 400) )
{
if($d>=30)
{
$d=1;
$m++;
break;
}
}
else
{
if($d>=29)
{
$d=1;
$m++;
break;
}
}
case '4':
case '6':
case '9':
case '11':
if($d>=31)
{
$d=1;
$m++;
}
break;
case '1':
case '3':
case '5':
case '7':
case '8':
case '10':
if($d>=32)
{
$d=1;
$m++;
}
break;
case '12':
if($d>=32)
{
$d=1;
$m=1;
$y++;
}
break;
}
$dn = date('D', strtotime($d.'-'.$m.'-'.$y));
if($d==1 && $dn=='Sun')
{
$d++;
}
if($d==1 && $dn=='Sat')
{
$d=$d+2;
}
$dayName = date('D', strtotime($d.'-'.$m.'-'.$y));
$planbookid = $this->planbook->get_planbook_id();
$d++;
$days++;
}
echo json_encode($result);
}
答案 4 :(得分:0)
您可以利用Carbon的diffInDaysFiltered()
和isWeekend()
方法。下面是Carbon文档的修改后的代码。
$daysForExtraCoding = $dt->diffInDaysFiltered(function(Carbon $date) {
return !$date->isWeekend();
}, $dt2);
echo $daysForExtraCoding; // 260