Skip to content

Instantly share code, notes, and snippets.

@beporter
Created May 10, 2017 14:29
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 beporter/5c30ac9273dba5bbb95c36f053861848 to your computer and use it in GitHub Desktop.
Save beporter/5c30ac9273dba5bbb95c36f053861848 to your computer and use it in GitHub Desktop.
PHP command line script that consumes a CSV dump from the [Pedometer++ iOS app](http://pedometerplusplus.com/) and reports total and average number of steps over the date range contained in the file.
#!/usr/bin/env php
<?php
/**
* Consumes a CSV file as exported by the
* [Pedometer++ iOS app](http://pedometerplusplus.com/) and reports your total
* steps and average steps per day for the range of dates contained in the file.
*
* beporter@users.sourceforge.net
* v1.0.0
* 2017-05-10
*
* Usage:
* $ ./pedometer.php steps.csv
*
*/
namespace beporter\Pedometer;
use \DateTime;
use \SplFileObject;
$csv = ($argv[1] ?? 'steps.csv');
$f = new SplFileObject($csv);
$f->setFlags(SplFileObject::READ_CSV);
$inputDateFormat = 'n/j/y|';
$outputDateFormat = 'Y-m-d';
$earliestDate = new DateTime();
$latestDate = new DateTime('1970-01-01 00:00:00');
$days = 0;
$total = 0;
foreach ($f as $row) {
list($date, $steps, $floors, $ascended, $descended) = $row;
$steps = intval($steps);
$date = DateTime::createFromFormat($inputDateFormat, $date);
if ($steps > 0) {
$days++;
$total += $steps;
$earliestDate = ($date < $earliestDate ? $date : $earliestDate);
$latestDate = ($date > $latestDate ? $date : $latestDate);
}
}
$f = null;
printf(
' Date Range: %s to %s' . PHP_EOL,
$earliestDate->format($outputDateFormat),
$latestDate->format($outputDateFormat)
);
printf(
' Total >0 days: %12d' . PHP_EOL,
$days
);
printf(
' Total steps: %12d' . PHP_EOL,
$total
);
printf(
'Avg steps per day: %14.1f' . PHP_EOL,
($total / $days)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment