Recreational Mathematics   
   home  |  library  |  contact
 Math Notes
 Math Programming [25]
 Regression [3]
 Data Mining [17]
 Notation [6]
 Linear Algebra [9]
 Stats & Prob [15]
 Math Cognition [5]
 Space & Physics [6]
 Formulas [5]
 Fun & Games [2]
 Haskell [1]
 Bayes Theory [1]
 Site News [0]
 Math Projects [5]
 Polynomials [1]
 Calculus [9]
 Number Theory [3]
 Optimization [2]
 Financial [1]

 Math Links
 Andrew Gelman
 Chance Wiki
 Daniel Lemire
 KD Knuggets
 Social Stats
 MySQL Performance
 Matthew Hurst
 Hal Daume III
 Math Notes >> Financial

PHP for doing income tax [Financial
Posted on February 27, 2012 @ 01:54:08 PM by Paul Meagher

I've slipped on my once-a-week blogging schedule because I've been busy the last couple of weeks migrating sites from an old server to a new server. In the last couple of days I've been busy getting ready for income tax season. Today I thought I would share with you a simple program I hacked together to date sort and total my gas receipts. The program could be used to date sort and total other types of receipts provided the text file it reads in is structured with the amount and date on each line with a colon separating them. Read the program comments for more details. The main feature of this program that is noteworthy is how to use of the array_multisort function to solve a real world problem. I haven't used this function in the past, however, it looks like a useful function to be aware of.


* @script tax-receipts.php
* @author Paul Meagher
* @modified Feb 27/2012
* @version 1.0
* Reads in a list of receipts with each line structured like this:
* amount : date
* Where "amount" has no dollar symbol, comma, or other extraneous symbols
* and "date" is structured in month-day format with zero padding (e.g., 02-01
* for feb 1).  Here is an excerpt of my gas-receipts.txt file:
* 20.00:05-06
* 32.03:07-13
* 23.87:07-14
* 45.08:09-11
* 23.73:08-15
* 34.00:09-16
* 18.00:06-30
* 63.10:04-21
* etc..
* So the idea is you enter all your unsorted receipts, for a given category of
* expenses, into a text file in this format then run this program on the receipts
* so it will sort them for you and calculate a total amount.  Save the output into
* your tax files so you have a nice record of your expenses for the tax man if he
* comes to audit you.
* The reason I created this program was because I had alot of gas receipts all
* thrown together into a pile and wanted to sort them by date to make it easier
* to potentially cross-check with my bank and credit card statements.  Using a
* program like this makes sense where you have a large number of unordered
* receipts in need of sorting.

// open receipts file (create text file for each category of expenses)
$fp fopen("gas-receipts.txt""r");

// declare and zero variables
$receipt_dates   = array();
$receipt_amounts = array();
$receipt_data    = array();

// read files items into array
while (!feof($fp)) {
$receipt_amount$receipt_date) = explode(":"$line);
$receipt_dates[]   = trim($receipt_date);
$receipt_amounts[] = trim($receipt_amount);
$receipt_data[]    = array('receipt_date'=>$receipt_date'receipt_amount'=>$receipt_amount);

// close file pointer

// sort the receipts by date then amount

// print out date sorted receipts and compute total amount
$num_receipts count($receipt_data);
$total_amount 0.0;
for (
$i=0$i $num_receipts$i++) {
$receipt_data[$i]['receipt_date']." : $".$receipt_data[$i]['receipt_amount']."<br />";
$total_amount += $receipt_data[$i]['receipt_amount'];

// and the total amount is
echo "Total Amount: $$total_amount";




php/Math Project
© 2011. All rights reserved.