Created
June 14, 2014 17:41
-
-
Save samuelkordik/81080697d3ae4f7d549e to your computer and use it in GitHub Desktop.
Simple Paystub Calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Simple Paystub Calculator | |
* Created by Samuel Kordik | |
* Copyright 2014 Samuel Kordik | |
* Licensed under MIT license -- feel free to use modify or extend as you wish, just attribute me in the source code. | |
* | |
* This single page form calculates paychecks based on a biweekly pay schedule using 2014 rates. | |
* It stores rate, # of deductions, pretax and posttax withdrawals in a cookie to simplify | |
* future calculations. | |
* | |
*/ | |
$paystub = (isset($_COOKIE['paystub'])) ? json_decode($_COOKIE['paystub'], true) : array('rate'=>false, 'pretax'=>0, 'posttax'=>0, 'deductions'=>2); | |
if (isset($_POST['rate'])) { | |
$paystub['deductions'] = filter_input(INPUT_POST, 'deductions', FILTER_SANITIZE_NUMBER_INT); | |
$withheld = 151.90 * $paystub['deductions']; | |
$paystub['pretax'] = filter_input(INPUT_POST, 'pretax'); | |
$paystub['posttax'] = filter_input(INPUT_POST, 'posttax'); | |
$week1hours = filter_input(INPUT_POST, 'week1hours', FILTER_SANITIZE_NUMBER_INT); | |
$week2hours = filter_input(INPUT_POST, 'week2hours', FILTER_SANITIZE_NUMBER_INT); | |
$paystub['rate'] = filter_input(INPUT_POST, 'rate'); | |
$week1 = ($week1hours > 40) ? ((($week1hours - 40) * 1.5) + 40)*$paystub['rate'] : $week1hours * $paystub['rate']; | |
$week2 = ($week2hours > 40) ? ((($week2hours - 40) * 1.5) + 40)*$paystub['rate'] : $week2hours * $paystub['rate']; | |
$total = $week1 + $week2; | |
$taxable = $total - $withheld - $paystub['pretax']; | |
if ($taxable < 436) { | |
$tax = 0.1*($taxable - 87); | |
} elseif ($taxable < 1506) { | |
$tax = 34.9 + (0.15*($taxable - 436)); | |
} elseif ($taxable < 3523) { | |
$tax = 195.4 + (0.25*($taxable - 1506)); | |
} elseif ($taxable < 7254) { | |
$tax = 699.65 + (0.28*($taxable - 3523)); | |
} elseif ($taxable < 15667) { | |
$tax = 1744.33 + (0.33*($taxable - 7254)); | |
} elseif ($taxable < 15731) { | |
$tax = 4520.62 + (0.35*($taxable - 15667)); | |
} else { | |
$tax = 4543.02 + (0.396*($taxable - 15731)); | |
} | |
if ($tax < 0) $tax = 0; | |
$tax = round($tax, 2); | |
$ss = round(($total - $paystub['pretax']) * 0.062, 2); | |
$mc = round(($total - $paystub['pretax']) * 0.0145, 2); | |
$pay = round($total - $tax - $ss - $mc - $paystub['posttax'], 2); | |
setcookie('paystub', json_encode($paystub)); | |
} | |
?> | |
<html> | |
<head> | |
<title>Instant Paystub Calculator</title> | |
<!-- Latest compiled and minified CSS --> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> | |
<!-- Optional theme --> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css"> | |
<!-- Latest compiled and minified JavaScript --> | |
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> | |
</head> | |
<body> | |
<div class="container"> | |
<form action="#" method="post"> | |
<h3 class="title">Enter Your Hours and Rate Here</h3> | |
<div class="well"> | |
<div class="form-group"> | |
<label for="week1hours">Week One Hours:<input class="form-control" type="number" id="week1hours" name="week1hours" required="" <?php echo ($week1hours) ? 'value="' . $week1hours .'"' : "";?>/></label> | |
</div> | |
<div class="form-group"> | |
<label for="week2hours">Week Two Hours:<input class="form-control" type="number" id="week2hours" name="week2hours" required="" <?php echo ($week2hours) ? 'value="' . $week2hours .'"' : "";?>/></label> | |
</div> | |
<div class="form-group"> | |
<label for="rate">Hourly Rate:<input type="text" id="rate" class="form-control" name="rate" | |
<?php echo ($paystub['rate']) ? 'value="' . $paystub['rate'] .'"' : "";?>/></label> | |
</div> | |
<div class="form-group"> | |
<label for="pretax">Pretax withdrawals:<input type="text" id="pretax" name="pretax" class="form-control" value="<?php echo($paystub['pretax']);?>"/></label></div> | |
<div class="form-group"> | |
<label for="posttax">After Tax withdrawals:<input type="text" id="posttax" name="posttax" class="form-control" value="<?php echo($paystub['posttax']);?>"/></label></div> | |
<div class="form-group"> | |
<label for="deductions">How many deductions?<select class="form-control" name="deductions" id="deductions"> | |
<?php | |
for($i=1; $i<=4; $i++) { | |
echo '<option val="'.$i.'"'; | |
echo ($paystub['deductions']==$i) ? ' selected>' : '>'; | |
echo $i . '</option>'; | |
} | |
?> | |
</select> | |
</label> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</div> | |
</form> | |
<?php | |
if (isset($_POST['rate'])) { | |
?> | |
<h3>Here's Your Results:</h3> | |
<div class="well"> | |
<strong>Total Pay: </strong>$<?php echo $total;?><br/> | |
<strong>Pretax withdrawals: </strong>$<?php echo $pretax;?><br/> | |
<strong>Income Tax Withheld: </strong>$<?php echo $tax;?><br/> | |
<strong>Social Security Tax: </strong>$<?php echo $ss;?><br/> | |
<strong>Medicare Tax: </strong>$<?php echo $mc;?><br/> | |
<strong>After tax withdrawals: </strong>$<?php echo $posttax;?><br/> | |
<strong>Net Pay: </strong>$<?php echo $pay;?><br/> | |
</div> | |
<?php } ?> | |
</div> | |
<p class="help-block">This page uses cookies to keep track of the rate you enter. No information is saved on the server.</p> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment