周末分割日期范围

时间:2013-06-11 09:11:49

标签: php date cakephp-2.2

我有一个CakePHP应用程序,它被用作我们公司日常活动的中心门户。这些包括各种预订系统和员工及其部门等信息。系统运行CakePHP 2.2

我目前正在实施的是一个招标人可以申请合同的系统。该合同有各种变化,这取决于在合同草案(截止日期)之前多长时间将合同草案移交给法人,以制定正式合同。然后系统“预订”#39;本合同成立几天的法人。那些日子里没有其他预订。

该系统运作良好,但它不会在周末拆分合同。它整天都在周末之前移动。让我们说,为了论证,我预订了一份合同草案,将于6月18日完成。系统确定总合同时间为3天。这意味着合同应该在一个周末结束,它应该阻止6月13日,14日和17日(不是18日,因为我需要在那时完成完成的文档)。但是,该系统将于6月12日至14日停止。

很抱歉长篇大论,但我宁愿让你知道完整的故事,而不仅仅是点点滴滴。这是我的系统代码:

$tenderDocument = $this->TenderDocument->read(null,$id);
$deadline = $tenderDocument['TenderDocument']['required_date'];
$deadline = $start = date('Y-m-d',strtotime('-1 day',strtotime($deadline)));
$start = date('Y-m-d',strtotime('-' . $tenderDocument['ContractForm']['time'],strtotime($deadline)));
$dates[] = $current = $start;
while($current < $deadline){  
    $current = date('Y-m-d',strtotime('+1 day',strtotime($current)));
    $dates[] = $current;  
}
$is_available = array();
$is_available = $this->TenderDocument->find('first',
    array(
        'conditions' => array(
            '(TenderDocument.start_date >= "' . $start . '" AND 
            TenderDocument.start_date < "' . $deadline . '" OR
            TenderDocument.start_date <= "' . $deadline . '" AND
            TenderDocument.required_date > "' . $deadline . '") AND
            TenderDocument.id != ' . $id
        )
    )
);
$is_weekend = false;
foreach($dates as $date) {
    if(date('l',strtotime($date)) == 'Sunday' || date('l',strtotime($date)) == 'Saturday') {
        $is_weekend = true;
    }
}
while(!empty($is_available) || $is_weekend) {
    $is_weekend = false;
    $dates = array();
    $deadline = date('Y-m-d',strtotime('-1 day',strtotime($deadline)));
    $start = date('Y-m-d',strtotime('-1 day',strtotime($start)));
    $dates[] = $current = $start;
    while($current < $deadline){  
        $current = date('Y-m-d',strtotime('+1 day',strtotime($current)));
        $dates[] = $current;
    }
    foreach($dates as $date) {
        if(date('l',strtotime($date)) == 'Sunday' || date('l',strtotime($date)) == 'Saturday') {
            $is_weekend = true;
        }
    }
    $is_available = $this->TenderDocument->find('first',
        array(
            'conditions' => array(
                '(TenderDocument.start_date >= "' . $start . '" AND 
                TenderDocument.start_date < "' . $deadline . '" OR
                TenderDocument.start_date <= "' . $deadline . '" AND
                TenderDocument.required_date > "' . $deadline . '") AND
                TenderDocument.id != ' . $id
            )
        )
    );
}

在一个不相关的说明中,有什么方法可以简化/最小化这段代码吗?

2 个答案:

答案 0 :(得分:1)

我希望我能正确理解您的问题,您需要计算合同的结束日期,并给出应该运行的天数,而不要将周末计算为工作日。

PHP的DateTime类可以大大简化您的代码。我认为这个功能可以帮到你: -

/**
 * Given the start date of a contract will return the end date, skipping weekends
 *
 * @param String $starDate format 'Y-m-d'
 * @param Int $numberOfDays Number of working days
 * @return Array of date strings
 */
function getDeadline($starDate, $numberOfDays)
{
    $start = \DateTime::createFromFormat('Y-m-d', $starDate);
    $interval = new DateInterval('P1D');
    $result = array();

    $i = 0;
    while($i < $numberOfDays){
        if((int)$start->format('N') < 6){
            $result[] = $start->format('Y-m-d');
            $i++;
        }
        $start->add($interval);
    }
    return $result;
}

$start = '2013-6-12';
var_dump(getDeadline($start, 4));

输出: -

array (size=4)
  0 => string '2013-06-12' (length=10)
  1 => string '2013-06-13' (length=10)
  2 => string '2013-06-14' (length=10)
  3 => string '2013-06-17' (length=10)

答案 1 :(得分:0)

我以完全不同的方式做到了。

我按日期收到了要求,创建了2个月的日期范围,然后从阵列中删除了已经预订了几天的所有星期六和星期日PLUS。然后我反向排序,并选择第一个所需的天数。

function _createDateRangeArray($strDateFrom,$strDateTo) {
    $aryRange=array();

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo>=$iDateFrom)
    {
        array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
        while ($iDateFrom<$iDateTo)
        {
            $iDateFrom+=86400; // add 24 hours
            array_push($aryRange,date('Y-m-d',$iDateFrom));
        }
    }
    return $aryRange;
}

public function confirm($id = null) {
    if($this->request->is('post')) {
        $this->TenderDocument->create();
        $data = array(
            'TenderDocument' => array(
                'id' => $id,
                'book_dates' => implode(',',$this->request->data['TenderDocument']['book_days'])
            )
        );
        if($this->TenderDocument->save($data)) {
            $this->_sendTenderDocument($id);
            $this->Session->setFlash('Tender document request saved.','default',array('class'=>'notification'));
            $this->redirect(array('action'=>'add'));
        } else {
            $this->Session->setFlash('There was an error saving your deadline. Please retry.','default',array('class'=>'error'));
        }
    }
    $this->TenderDocument->id = $id;
    if (!$this->TenderDocument->exists()) {
        throw new NotFoundException(__('Invalid tender document'));
    }
    $tenderDocument = $this->TenderDocument->read(null,$id);
    $deadline = $tenderDocument['TenderDocument']['required_date']; // Deadline date as entered on form
    $deadline = $start = date('Y-m-d',strtotime('-1 day',strtotime($deadline))); // adjusted deadline
    $start = date('Y-m-d',strtotime('-3 months',strtotime($deadline))); // adjusted start
    $period = $this->_createDateRangeArray($start,$deadline);
    $TenderDocuments = $this->TenderDocument->find('list',
        array(
            'fields' => array('TenderDocument.book_dates')
        )
    );
    $dates = array();
    foreach($TenderDocuments as $TenderDocument) {
        $doc_dates = explode(',',$TenderDocument);
        foreach($doc_dates as $doc_date) {
            $dates[] = $doc_date;
        }
    }
    foreach($period as $key=>$value) {
        if(date('l',strtotime($value)) == 'Saturday' || date('l',strtotime($value)) == 'Sunday' || in_array($value,$dates)) {
            unset($period[$key]);
        }
    }
    rsort($period);
    for($k = 0; $k <= $tenderDocument['ContractForm']['time']; $k++) {
        $book_days[] = $period[$k];
    }
    $this->set('TenderDocument',$this->TenderDocument->read(null,$id));
    $this->set('book_days',$book_days);
}

如果有任何方法可以简化此代码并加快速度,请随时发表评论。