Created
August 12, 2014 04:25
-
-
Save iansltx/969b667d91c7b219f22b to your computer and use it in GitHub Desktop.
Toggl CSV -> grouped by description and project + decimal hours QDPA
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 | |
$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