Skip to content

Instantly share code, notes, and snippets.

@joewoodhouse
Forked from salgo/convert.php
Last active December 13, 2016 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joewoodhouse/967e19ecc5c1fa46ca5354fcb84ba168 to your computer and use it in GitHub Desktop.
Save joewoodhouse/967e19ecc5c1fa46ca5354fcb84ba168 to your computer and use it in GitHub Desktop.
Convert Barclaycard commercial to freeagent CSV format
<?php
$row = 0;
if ($argc != 4) {
die('Expected 3 arguments: php convert.php $input_file $output_file $after_date');
}
date_default_timezone_set('Europe/London');
$input = $argv[1];
$output = $argv[2];
// Only process rows after this date (supplied on the command line)
$after_date = DateTime::createFromFormat('d/m/Y', $argv[3]);
if (($handle = fopen($input, 'r')) !== FALSE && ($ohandle = fopen($output, 'w'))) {
while (($data = fgetcsv($handle, 2000, ",")) !== FALSE) {
// Each line must have 21 columns, else give up
if (count($data) != 21) {
die('Expected 21 columns');
}
$row++;
// If this is the first row, do nothing - it's the header row?
if ($row == 1) {
continue;
}
// Parse the date from column 3 (index 2)
$date = DateTime::createFromFormat('d.m.Y', $data[2]);
// If the date is less than or equal to the "after date", skip this row
if ($date <= $after_date) {
echo "Skipping after date\n";
continue;
}
// Get the value in column 5 (index 5) and multiply by -1
$value = $data[4] * -1;
// Write out the line. The line only has 3 columns:
// date, value (above), and the contents of column 4 (index 3)
$new = [
$date->format('d/m/Y'), $value, $data[3]
];
fputcsv($ohandle, $new);
}
fclose($handle);
fclose($ohandle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment