Skip to content

Instantly share code, notes, and snippets.

@SimonS
Last active January 1, 2016 19:28
Show Gist options
  • Save SimonS/8190148 to your computer and use it in GitHub Desktop.
Save SimonS/8190148 to your computer and use it in GitHub Desktop.
Hacking around my streak with the dailymile API
<?php
require 'vendor/autoload.php';
$client = new DailymilePHP\Client;
$entries = $client->getEntries(['username' => 'simons', 'page' => 'all', 'since' => '1325462400']);
echo 'Entries: ' . count($entries). "\n";
$runningEntries = array_filter($entries, function($entry) {
return isset($entry['workout']) && $entry['workout']['activity_type'] === "Running";
});
echo 'Running Entries: ' . count($runningEntries). "\n";
$aggregatedEntries = array();
foreach($runningEntries as $entry)
{
$date = explode('T', $entry['at'])[0];
if (!isset($aggregatedEntries[$date]))
{
$aggregatedEntries[$date] = 0;
}
if (isset($entry['workout']['distance']))
{
$distance = $entry['workout']['distance'];
if ($distance['units'] !== 'miles')
$distance['value'] = round($distance['value'] * 0.621371192, 2);
$aggregatedEntries[$date] += $distance['value'];
}
}
echo 'Logged Running Days: ' . count($aggregatedEntries). "\n";
echo 'Total Logged Mileage: ' . array_sum($aggregatedEntries). "\n";
echo 'Average Run Length: ' . round(array_sum($aggregatedEntries) / count($runningEntries), 2). "\n";
echo 'Average Daily Mileage: ' . round(array_sum($aggregatedEntries) / count($aggregatedEntries), 2). "\n";
$dates = array_keys($aggregatedEntries);
sort($dates);
$currentDate = $dates[0];
$endDate = date('Y-m-d');
$missedDays = 0;
while($currentDate != $endDate)
{
if (!isset($aggregatedEntries[$currentDate]))
{
$missedDays++;
}
$dateArray = explode('-', $currentDate);
$currentDate = date('Y-m-d', mktime(0, 0, 0, $dateArray[1], ++$dateArray[2], $dateArray[0]));
}
echo 'Total unlogged days: ' . $missedDays . "\n";
echo 'Adjusted mileage: ' . (array_sum($aggregatedEntries) + $missedDays) . "\n";
echo 'Adjusted average daily mileage: ' . round((array_sum($aggregatedEntries) + $missedDays) / (count($aggregatedEntries) + $missedDays), 2) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment