Calculating PAYE in PHP and JavaScript

Felix Ivance Runye
3 min readOct 12, 2023
Photo by Jon Tyson on Unsplash

Welcome to today’s article where we’ll dive into the world of taxation and explore how to calculate Pay As You Earn (PAYE) for employees in Kenya. Whether you’re an employer, employee, or simply curious about how your income tax is computed, understanding the mechanics of PAYE can be both enlightening and beneficial.

In Kenya, PAYE is the tax deducted from an employee’s salary by their employer and remitted to the Kenya Revenue Authority (KRA). It’s a crucial part of the country’s revenue collection, and knowing how it works can help you plan your finances more effectively.

In this article, we’ll explore two functions: one in PHP and the other in JavaScript, to calculate PAYE based on the current tax rates (as of July 2023). We’ll break down the code and provide explanations to help you understand how these functions work.

The PHP Function: calculatePayee

Let’s start with the PHP function calculatePayee. This function takes two parameters: salaryAmount and nssfAmount. salaryAmount is the employee's gross monthly salary, while nssfAmount represents the National Social Security Fund (NSSF) deduction.

public function calculatePayee($salaryAmount, $nssfAmount){
$tax = 0;
$remaining = $salaryAmount - $nssfAmount;

if($remaining > 24000){
$tax += 24000 * (10/100);
$remaining -= 24000;

if($remaining > 0){
$amountToTax = ($remaining < 8333) ? $remaining : 8333 ;
$tax += $amountToTax * (25/100);
$remaining -= $amountToTax;
}

if($remaining > 0){
$amountToTax = ($remaining < 467667) ? $remaining : 467667 ;
$tax += $amountToTax * (30/100);
$remaining -= $amountToTax;
}

if($remaining > 0){
$amountToTax = ($remaining < 300000) ? $remaining : 300000 ;
$tax += $amountToTax * (32.5/100);
$remaining -= $amountToTax;
}

if($remaining > 0){
$tax += $remaining * (35/100);
$remaining = 0;
}
}
return $tax;
}
image from https://www.aren.co.ke/payroll/taxrates.htm#ftn1

The JavaScript Function

Now, let’s create a JavaScript function to achieve the same result. JavaScript is a versatile language often used in web applications, so understanding how to calculate PAYE in JavaScript can be valuable.

Here’s the JavaScript function:

function calculatePayee(salaryAmount, nssfAmount) {
let tax = 0;
let remaining = salaryAmount - nssfAmount;

if (remaining > 24000) {
tax += 24000 * (10 / 100);
remaining -= 24000;

if (remaining > 0) {
let amountToTax = (remaining < 8333) ? remaining : 8333;
tax += amountToTax * (25 / 100);
remaining -= amountToTax;
}

if (remaining > 0) {
let amountToTax = (remaining < 467667) ? remaining : 467667;
tax += amountToTax * (30 / 100);
remaining -= amountToTax;
}

if (remaining > 0) {
let amountToTax = (remaining < 300000) ? remaining : 300000;
tax += amountToTax * (32.5 / 100);
remaining -= amountToTax;
}

if (remaining > 0) {
tax += remaining * (35 / 100);
remaining = 0;
}
}
return tax;
}

This JavaScript function works in a similar way to the PHP function. It calculates the PAYE based on the same income brackets and rates.

With these two functions at your disposal, you can easily calculate PAYE for employees in Kenya, whether you’re working on a web application or simply want to understand the tax deductions from your salary.

Understanding how PAYE is calculated empowers you to make informed financial decisions and ensures compliance with tax regulations. So, next time you receive your payslip.

If you have any suggestions, please comment or contribute to this gist

--

--

Felix Ivance Runye

I am a Software Engineer, and an Adventurer, who is passionate about cycling, biking, and reading. Always on the lookout for new challenges.