Calculate VAT GST In PHP (Simple Examples)

Welcome to a quick tutorial on how to calculate VAT and GST in PHP. Working on your own eCommerce platform in PHP? Trying to figure out how tax should be calculated?

  • To calculate the grand total with tax in PHP – $grand = $total  * ((100 + TAX PERCENT) / 100);
  • To get the taxable amount – $tax = $total * (TAX PERCENT / 100);

That should cover the basics, but read on if you need more examples!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP TAX CALCULATIONS

All right, let us now get into the examples of how to calculate GST and VAT in PHP.

 

1) TAX AMOUNT FOR ALL ITEMS

1-items.php
<?php
// (A) DUMMY ITEMS (NAME => PRICE)
$items = [
  "Foo Bar" => 12.34,
  "Goo Bar" => 8.76,
  "Poo Bar" => 34.56
];

// (B) CALCULATE TOTALS + TAX
$taxrate = 9; // TAX PERCENTAGE
$subtotal = 0;
$tax = 0;
foreach ($items as $name=>$price) {
  $itemtax = $price * ($taxrate / 100);
  $subtotal += $price;
  $tax += $itemtax;
  echo "$name (Price) - $price<br>";
  echo "$name (Taxable) - $itemtax<br>";
}
 
// (C) GRAND TOTAL
$grand = $subtotal + $tax;
echo "Subtotal - $subtotal<br>";
echo "Add Tax - $tax<br>";
echo "Grand Total - $grand<br>";

Yep, this example is rather intimidating at first, but keep calm and look carefully.

  • We are basically looping through the list of items.
  • Calculating the taxable amount for each item.
  • Then add them together to get the sub-total, total taxable, and grand total.

 

 

2) CALCULATE TOTAL TAXABLE

2-total.php
// (B) CALCULATE TOTALS + TAX
$taxrate = 9; // TAX PERCENTAGE
$subtotal = 0;
$tax = 0;
foreach ($items as $name=>$price) {
  $subtotal += $price;
}
 
// (C) GRAND TOTAL
$tax = $subtotal * ($taxrate / 100);
$grand = $subtotal + $tax;
echo "Subtotal - $subtotal<br>";
echo "Add Tax - $tax<br>";
echo "Grand Total - $grand<br>";

If you don’t need to calculate the taxable amount for each item, this is the faster way – Simply add the prices of all the items to get the sub-total, then calculate the tax and grand total.

 

3) ALTERNATE WAY TO CALCULATE TAX

3-alternate.php
// (C) GRAND TOTAL
$grand = $subtotal * ((100 + $taxrate) / 100);
$tax = $grand - $subtotal;
echo "Subtotal - $subtotal<br>";
echo "Add Tax - $tax<br>";
echo "Grand Total - $grand<br>";

Lastly, this is another way to calculate the tax – We directly calculate the grand total and work backward to get the taxable amount.

 

 

EXTRA) ROUNDING OFF TO N DECIMAL PLACES

$price = 123.456789;
 
// (A) ROUND OFF TO 2 DECIMAL PLACES
$rounded = round($price, 2); // 123.46

// (B) CEILING - ALWAYS ROUND UP TO NEXT WHOLE NUMBER
$ceiling = ceil($price); // 124

// (C) FLOOR - ALWAYS ROUND DOWN TO LAST WHOLE NUMBER
$ceiling = ceil($price); // 123

Yep, the native PHP rounding functions should be enough in most cases.

 

EXTRA) ROUND TO NEAREST QUARTER DECIMAL

// (A) THE AMOUNT
$price = 123.47;
 
// (B) WHOLE & DECIMAL PARTS
$whole = floor($price);
$decimal = $price - $whole;
 
// (C) ROUND OFF
// 0.01 TO 0.24- ROUND DOWN TO 0.
// 0.25 TO 0.49 – ROUND UP TO 0.5.
// 0.51 TO 0.74 – ROUND DOWN TO 0.5.
// 0.75 TO 0.99 – ROUND UP TO 1.
if ($decimal < 0.25) { $price = $whole; }
else if ($decimal >= 0.25 && $decimal <0.75) { $price = $whole + 0.5; }
else { $price = $whole + 1; }
 
echo $price;

Take note that some companies or regions may have very specific rounding rules. For example, I once worked with a POS that rounded off to the nearest 5 cents:

  • 0.01 to 0.24- Round down to 0.
  • 0.25 to 0.49 – Round up to 0.5.
  • 0.51 to 0.74 – Round down to 0.5.
  • 0.75 to 0.99 – Round up to 1.

So ask your client if you are not sure. Consulting an accountant is the best way to get the rounding right.

 

EXTRA BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *