0

I have written an income tax band calculation with PHP and it looks like the code works for some condition and the rest return inaccurate figures. Below is my PHP code for the calculation. Kindly ignore the currency. it could be in UDS

Am trying to calculate the income tax on the amount eg. 1000 with the following tax bands below

// first  3,828 GHS ==> Nil tax
// next   1,200 GHS ==> 5% tax
// next   1,440 GHS ==> 10% tax
// next  36,000 GHS ==> 17.5% tax
// next 197,532 GHS ==> 25% tax
// over 240,000 GHS ==> 30% tax


$income_tax_amount = 0;

        //the tops of each tax band
        $band1_top = 3828;
        $band2_top = 1200;
        $band3_top = 1440;
        $band4_top = 36000;
        $band5_top = 197532;
        $band6_top = 240000;
        //no top of band 4

        //the tax rates of each band
        $band1_rate = 0.0;
        $band2_rate = 0.05;
        $band3_rate = 0.10;
        $band4_rate = 0.175;
        $band5_rate = 0.25;
        $band6_rate = 0.30;

        $starting_income = 1000; //set this to your income
        $band1 = $band2 = $band3 = $band4 = $band5 = $band6 =0;
        if($starting_income >= $band1_top) {
            $band1 = ($band1_rate) *  $band1_top - ($band1_top);
        }
        if($starting_income >= $band2_top) {
            $band5 = ($band2_rate) *  $band2_top - ($band2_top);
        }
        if($starting_income >= $band4_top) {
            $band4 = ($band4_rate) *  $band4_top;
        }
        if($starting_income >= $band3_top) {
            $band3 = ($band3_rate) *  $band3_top;
        }
        if($starting_income >= $band2_top) {
            $band2 = ($band2_rate) *  $band2_top;
        }
        if($starting_income >= $band1_top) {
            $band1 = ($band1_rate) *  $band1_top;
        }
        
        $income_tax_amount = $band1 + $band2 + $band3 + $band4 + $band5 + $band6;
            
        echo $income_tax_amount;

The image below illustrates the picture am trying to paint enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
user3315848
  • 81
  • 1
  • 3
  • 13
  • 1
    Your code doesn't make much sense to me. Perhaps that's because you haven't explained how the tax should be calculate? Only by looking up the currency used in the image I can deduce it must be the Ghanaian cedi from Ghana, and therefore the tax system used there. – KIKO Software Dec 20 '20 at 10:19
  • You are right @KIKO Software without your hint that the country is Ghana I coudn't have written my answer below. Thanks for that! – rf1234 Dec 20 '20 at 11:53
  • @rf1234 you are right. Sorry guys i will update the code and explanation to make it clearer and understandable – user3315848 Dec 20 '20 at 13:17
  • Are you trying to calculate the marginal tax rate? If so you must subtract the lower bands from the total income after calculating tax on it or else you will be taxing the first few dollars over and over. – Josh J Dec 21 '20 at 12:21

1 Answers1

0

This function will calculate the tax according to the rules defined on this page: https://home.kpmg/xx/en/home/insights/2020/02/flash-alert-2020-036.html look for "Residents: Rates & Bands"

function calc_income_tax($income) {

    // see this page for the bands in Ghana:
    // https://home.kpmg/xx/en/home/insights/2020/02/flash-alert-2020-036.html
    // look for "Residents: Rates & Bands"

    // first  3,828 GHS ==> Nil tax
    // next   1,200 GHS ==> 5% tax
    // next   1,440 GHS ==> 10% tax
    // next  36,000 GHS ==> 17.5% tax
    // next 197,532 GHS ==> 25% tax
    // over 240,000 GHS ==> 30% tax

    // $band_arr has the top amounts of each band in Ghana currency in descending order
    // We have 5 thresholds to higher tax rates and 5 rates for each of the thresholds
    $band_arr = [240000, 42468, 6468, 5028, 3828];
    $rate_arr = [0.3, 0.25, 0.175, 0.1, 0.05];
    $income_tax_amount = 0;

    foreach ($band_arr as $key => $threshold) {
        if ( $income > $threshold ) {
            $exceeding_income = $income - $threshold;
            $income_tax_amount += ( $exceeding_income * $rate_arr[$key] );
            $income = $threshold;
        }
    }

    return $income_tax_amount;
}

I expanded this a little more to echo the values for the various tax brackets depending on user input. This code uses the function above and you find a sandbox example here: http://sandbox.onlinephpfunctions.com/code/bec1084eb0d70ce8907979af65ab02c3eaa16b22

And here is the additional code as well:

$input_income = 340000; //as entered by the user online

$rate_arr = ["0%", "5%", "10%", "17.5%", "25%", "30%"];
$band_arr = [3828, 5028, 6468, 42468, 240000];
for  ($i=0; $i < count($band_arr); $i++ ) {
    if ( $band_arr[$i] >= $input_income ) {
        unset($band_arr[$i]);
    }
}
$band_arr[] = $input_income;
$band_arr = array_values($band_arr); //reorganize array to close gaps in index

$total_tax = calc_income_tax($input_income);
echo "total income tax: ".number_format($total_tax). "<br>";

$previous_bands_tax = 0;
foreach ($band_arr as $key => $threshold) {
    $band_tax = calc_income_tax($threshold) - $previous_bands_tax;
    $previous_bands_tax += $band_tax;
    if ( $key == 0 ) {
        echo "first ".number_format($threshold)."; rate: "
            .$rate_arr[$key].": ".number_format($band_tax)."<br>";
    } else {
        echo "next ".number_format($threshold - $band_arr[$key-1])."; rate: "
            .$rate_arr[$key].": ".number_format($band_tax)."<br>";
    }
}
rf1234
  • 1,510
  • 12
  • 13