dongxuying7583 2019-01-23 04:32
浏览 22

php失败场景,用于计算给定日期和持续时间的开始/结束日期

I am working on a project management application for my client. The requirement was to create a task & assign to a given employee. I need to calculate start date if end date is given & viceversa. I hope that I have explained my code clearly with the comments.

The issue is that the response I get is incorrect only sometimes. I could not find the scenario in which it gives wrong date. Can you check my code & let me know if you can find the scenario in which it gives wrong date please so that I can fix.

I am given with restricted access to the server as this code is being executed through a despatcher & I do not have access to the log files.

Please help me solve the issue. Your help is much appreciated.

/**
    *** PARAMETERS ***
    * $givenDate - Carbon object
    * $totalDays - no. of days required/assigned for the task
    * $direction - calculate the date either forward or backward from the given date

    *** VARIABLES ***
    * $taskdays
        -> Array of dates in which tasks already assigned to the particular employee
    * $leaves
        -> Array of dates in which tasks the particular employee has applied for leave & approved
    * $holidays
        -> Array of dates - planned holidays for a particular branch of a company
        -> Since branches of the company are located in different countries, applicable holidays are also set based on the country of the branch location.
    * $weekends
        -> Array of days(eg., $days=array('Saturday', 'Sunday')) - weekends for a particular branch of a company.
        -> Since branches of the company are located in different countries, weekends are followed based on the country of the branch location.
        -> eg., Qatar(Friday & Saturday), India(Saturday & Sunday), Bangladesh(Sunday Only)
**/

private function calculateStartEndDates($givenDate, $totalDays=1, $direction='forward') {
    $taskdays = $this->getTaskAssignedDays($employeeId);
    $leaves = $this->getApprovedLeaveDays($employeeId);
    $holidays = $this->getHolidays($companyBranchId);
    $weekends = $this->getWeekends($companyBranchId);

    while($totalDays > 0) {
        if($direction == 'backward') {
            $givenDate = $givenDate->subDays(1);
        } else {
            $givenDate = $givenDate->addDays(1);
        }
        $currentDate = $givenDate->format('Y-m-d');
        if(!in_array($currentDate, $taskdays) && !in_array($currentDate, $leaves) && !in_array($currentDate, $holidays) && !in_array($givenDate->format('l'), $weekends)) {
            $totalDays--;
        }
    }
    return $givenDate;
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)