Skip to content

Instantly share code, notes, and snippets.

@salgo

salgo/convert.php

Last active Jun 6, 2017
Embed
What would you like to do?
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' . "\n");
}
date_default_timezone_set('Europe/London');
$input = $argv[1];
$output = $argv[2];
$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) {
if (count($data) != 21) {
die('Expected 21 columns');
}
$row++;
if ($row == 1) {
continue;
}
$date = DateTime::createFromFormat('d/m/Y|', $data[2]);
if ($date < $after_date) {
echo "Skipping because " . $date->format('d/m/Y') . " is before " . $after_date->format('d/m/Y') . "\n";
continue;
}
$value = $data[4] * -1;
$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