'Leave accrual function in PHP

I am developing a system for leave management where the leaves get accrued each month. On the 1st day of each month, the leave for the previous month gets accrued. So for an employee who joined on Jan 1st leave will be 0. In Feb he will have 1.5 and so on. The leave for Dec will get accrued next year on Jan 1st. There is also another condition to check that if the employee has joined after the 15th of the month, he will have only 1 leave for that month. I have tried a function but I don't have an idea how to get the leave for next year. There is a separate leave carry forwarding of 6 leaves to next year. For which I have written a cron to run on Jan 1st. Here the problem is that as I am using the PHP date function to get the current date, in the next year Jan current month count will be 1. So I won't get the previous year's Dec accrued leave.

public function getAccruedLeave($joined_date, $leaveData, $isPrevYear = false)
    {
        $total = floatval(0);
        $joinedYear = (int)date('Y', strtotime($joined_date));
        if ($isPrevYear) {
            $currentYear =  (int)date("Y", strtotime("-1 year"));
        } else {
            $currentYear = (int)date('Y', time());
        }
        if ($joinedYear > $currentYear) {
            return 0;
        }
        $joinedMonthNo = (int)date('m', strtotime($joined_date));
        if ($isPrevYear) {
            $currentMonthNo = 12;
        } else {
            $currentMonthNo = (int)date('m', time());
        }
        $accrualCount = floatval($leaveData['accrual_count']);
        $dtJoinAftrLeave = floatval($leaveData['dete_of_joining_after_leave']);
        $dtJoinAfter = floatval($leaveData['dete_of_joining_after']);

        if (
            ($joinedYear === $currentYear) &&
            (int)date("d", strtotime($joined_date)) > $dtJoinAfter
        ) {
            if ($dtJoinAftrLeave > 0) {
                $total = (($currentMonthNo - $joinedMonthNo - 1) * $accrualCount) + $dtJoinAftrLeave;
            } else {
                $total = ($currentMonthNo - $joinedMonthNo - 1) * $accrualCount;
            }
        } elseif (($joinedYear === $currentYear)) {
            $total = ($currentMonthNo - $joinedMonthNo) * $accrualCount;
        } else {
            $total = ($currentMonthNo - 1) * $accrualCount;
        }

        

        return $total;
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source