Skip to content

Instantly share code, notes, and snippets.

@iansltx
Created August 12, 2014 04:25
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 iansltx/969b667d91c7b219f22b to your computer and use it in GitHub Desktop.
Save iansltx/969b667d91c7b219f22b to your computer and use it in GitHub Desktop.
Toggl CSV -> grouped by description and project + decimal hours QDPA
<?php
$filename = $argv[1];
if (!file_exists($filename))
die("file not found\n");
$rows = explode("\n", str_replace(["\r\n", "\r"], "\n", trim(file_get_contents($filename))));
$headers = str_getcsv(array_shift($rows));
$data = array_map(function($row) use ($headers) {return array_combine($headers, str_getcsv($row));}, $rows);
usort($data, function($a, $b) {return strtotime($a['Start date']) > strtotime($b['Start date']) ? 1 : -1;});
$entries = [];
foreach ($data as $entry) {
if (!array_key_exists($entry['Project'], $entries))
$entries[$entry['Project']] = [];
if (!array_key_exists($entry['Description'], $entries[$entry['Project']]))
$entries[$entry['Project']][$entry['Description']] = 0;
$timeParts = explode(':', $entry['Duration']);
$entries[$entry['Project']][$entry['Description']] += $timeParts[0] * 3600 + $timeParts[1] * 60 + $timeParts[2];
}
foreach ($entries as $project => $tasks) {
print("=== $project ===\n");
print(implode("\n", array_keys($tasks)));
print("\n\n");
print(implode("\n", array_map(function($seconds) {
return round($seconds / 3600) . ':' .
str_pad(round(($seconds % 3600) / 60), 2, '0', STR_PAD_LEFT) . ':' .
str_pad($seconds % 60, 2, '0', STR_PAD_LEFT);
}, $tasks)));
print("\n\n");
print(number_format(array_sum($tasks) / 3600, 2) . "\n\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment