Loading..

Calculate VAT with PHP 0 0

Last updated on Dec 19, 2019 04:08 in RESOURCES
Posted By reza saghafi

If you’re programming an eCommerce site, sooner or later you’ll need to calculate VAT. Although it doesn’t change very often, this function allows you to set the VAT to anything you like. The $isVATInclusive is a boolean flag and determines whether the $value you pass already has VAT added or not. This is because sometimes you store prices inclusive of VAT and need to reverse calculate it. The value returned is the VAT only.

function vat($value, $vat, $isVATInclusive) {

    if($isVATInclusive === false) {

        $value = ($value * $vat) / 100;

    }
    else {

        $percent        = 100 + $vat;
        $newValue       = $value / $percent;
        $newValue      *= 100;
        $value         -= $newValue;
        

    }

    return number_format($value, 2);

}

** The time is base on America/New_York timezone