Skip to content

Instantly share code, notes, and snippets.

@dastagirkhan
Last active September 17, 2021 10:20
Show Gist options
  • Select an option

  • Save dastagirkhan/1bdb62a678804387c68ccb479efafc5f to your computer and use it in GitHub Desktop.

Select an option

Save dastagirkhan/1bdb62a678804387c68ccb479efafc5f to your computer and use it in GitHub Desktop.
<?php
// get net price
$vat = 20;
$vat_divisor = 1 + ($vat/100);
$price_gross = 5;
$price_net = number_format($price_gross/$vat_divisor,2);
//Adding VAT to the price of a product
//The VAT rate / percentage.
$vat = 20;
//The price, excluding VAT.
$priceExcludingVat = 10;
//Calculate how much VAT needs to be paid.
$vatToPay = ($priceExcludingVat / 100) * $vat;
//The total price, including VAT.
$totalPrice = $priceExcludingVat + $vatToPay;
//Print out the final price, with VAT added.
//Format it to two decimal places with number_format.
echo number_format($totalPrice, 2);
//Determining the VAT rate of a gross price
//The VAT rate.
$vat = 21.5;
//Divisor (for our math).
$vatDivisor = 1 + ($vat / 100);
//The gross price, including VAT.
$price = 20;
//Determine the price before VAT.
$priceBeforeVat = $price / $vatDivisor;
//Determine how much of the gross price was VAT.
$vatAmount = $price - $priceBeforeVat;
//Print out the price before VAT.
echo number_format($priceBeforeVat, 2), '<br>';
//Print out how much of the gross price was VAT.
echo 'VAT @ ' . $vat . '% - ' . number_format($vatAmount, 2), '<br>';
//Print out the gross price.
echo $price;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment