Skip to content

Instantly share code, notes, and snippets.

@cobolfoo
Created May 11, 2015 01:17
Show Gist options
  • Save cobolfoo/6868b0558f7f9c6be831 to your computer and use it in GitHub Desktop.
Save cobolfoo/6868b0558f7f9c6be831 to your computer and use it in GitHub Desktop.
Converting XML data about health rate from Apple Watch to CSV
<?php
$xml = xml_parser_create();
xml_parse_into_struct($xml, file_get_contents("export.xml"),$values,$indexes);
print '"Date/Time","Average","Minimum","Maximum"'."\r\n";
foreach($values as $val) {
if (isset($val["attributes"]) == false) {
continue;
}
if ($val["attributes"]["TYPE"] != "HKQuantityTypeIdentifierHeartRate") continue;
$date = $val["attributes"]["STARTDATE"];
sscanf($date, "%04s%02s%02s%02s%02s%02s-0400", $year, $month, $day, $hour, $minute, $second );
$readable_date = $year."-".$month."-".$day." ".$hour.":".$minute.":".$second;
$min = round($val["attributes"]["MIN"] * 60);
$max = round($val["attributes"]["MAX"] * 60);
$avg = round($val["attributes"]["AVERAGE"] * 60);
print '"'.$readable_date.'","'.$min.'","'.$avg.'","'.$max.'"'."\r\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment