如果为空则停止调用函数

时间:2017-07-19 09:32:21

标签: php function

如果我的某个字段为空,我怎么能停止调用该函数?因为我有一个计算两个字段之间的天数的函数,所以如果我的字段为空,则计数结果显示为“12,000”,就像那样。

示例:

$start = ("");
$end =("2017-10-19");

$date = getWorkingDays($start, $end);

这是使用的函数,但每当$ start大于$ end时,我得到一个错误的结果。我该怎么办呢?

function getWorkingDays($startDate,$endDate ){
        // do strtotime calculations just once
        $startDate = strtotime($startDate);
        $endDate = strtotime($endDate);

        //The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24
        //We add one to inlude both dates in the interval.
        $days = ($endDate - $startDate) / 86400 + 0;

        $no_full_weeks = floor($days / 7);
        $no_remaining_days = fmod($days, 7);

        //It will return 1 if it's Monday,.. ,7 for Sunday
        $the_first_day_of_week = date("N", $startDate);
        $the_last_day_of_week = date("N", $endDate);

        // If one of the value is empty it will return "0"
         if ($startDate == '' || $endDate == '')
                return "0"; // Default value

        //---->The two can be equal in leap years when february has 29 days, the equal sign is added here
        //In the first case the whole interval is within a week, in the second case the interval falls in two weeks.
        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 {
            // (edit by Tokes to fix an edge case where the start day was a Sunday
            // and the end day was NOT a Saturday)

            // the day of the week for start is later than the day of the week for end
            if ($the_first_day_of_week == 7) {
                // if the start date is a Sunday, then we definitely subtract 1 day
                $no_remaining_days--;

                if ($the_last_day_of_week == 6) {
                    // if the end date is a Saturday, then we subtract another day
                    $no_remaining_days--;
                }
            }
            else {
                // the start date was a Saturday (or earlier), and the end date was (Mon..Fri)
                // so we skip an entire weekend and subtract 2 days
                $no_remaining_days -= 2;
            }
        }

        //The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder
        //---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it
        $workingDays = $no_full_weeks * 5;
        if ($no_remaining_days > 0 )
        {
          $workingDays += $no_remaining_days;
        }

        return $workingDays;
    }

$start = ("2017-04-21");
$end = ("2017-04-17");

$date = getWorkingDays($start, $end);
    echo $date;

1 个答案:

答案 0 :(得分:1)

您只需检查$start$end是否为空。

调用你的函数时:

$start = ("");
$end =("2017-10-19");

if ($start != '' && $end != '') {
    $date = getWorkingDays($start, $end);
} else {
    $date = 0; // Default value
}

或者在你的函数声明中:

function getWorkingDays($start, $end) {
    if ($start == '' || $end == '')
        return 0; // Default value
    /* Here is the rest of your function */
}