Example #1
0
    $arr = str_getcsv($row);
    if (empty($col_names)) {
        $col_names = $arr;
    } else {
        $line = array();
        foreach ($arr as $i => $val) {
            $line[$col_names[$i]] = $val;
        }
        // Clean up data & fill in the blank
        if (!isset($line['distance_meters']) || empty($line['distance_meters'])) {
            $line['distance_meters'] = convertDistanceToMeters($line['distance']);
        }
        if (!isset($line['time_seconds']) || empty($line['time_seconds'])) {
            $line['time_seconds'] = convertTimeToSeconds($line['time']);
        }
        $line['date'] = cleanupDate($line['date']);
        $line['pace'] = formatPace($line['time_seconds'], $line['distance_meters']);
        $races_arr[] = $line;
    }
}
// var_dump($races_arr); // debug
$json_file = explode(".", $csv_file)[0] . '.json';
if (!file_exists($json_file)) {
    $json_fh = fopen($json_file, 'w');
    fwrite($json_fh, json_encode($races_arr));
    echo count($races_arr) . " lines written to {$json_file}\n";
} else {
    throw new InvalidArgumentException("JSON output file already exists: {$json_file}.");
}
function cleanupDate($input)
{
<?php

$mbox = imap_open("{imap.zpush.org:143/notls/norsh}INBOX", "username", "password");
date_default_timezone_set("UTC");
$result = imap_fetch_overview($mbox, "1:*", 0);
foreach ($result as $overview) {
    if (isset($overview->date)) {
        printf("%s\n", $overview->date);
        printf("%s\n", cleanupDate($overview->date));
    }
    if (isset($overview->udate)) {
        printf("%s\n", $overview->udate);
    }
}
imap_close($mbox);
function cleanupDate($receiveddate)
{
    if (is_array($receiveddate)) {
        // Header Date could be repeated in the message, we only check the first
        $receiveddate = $receiveddate[0];
    }
    printf("%s\n", preg_replace("/\\(.*\\)/", "", $receiveddate));
    printf("%s\n", preg_replace('/\\(.*\\)/', "", $receiveddate));
    printf("%s\n", preg_replace("/\\(.*\\)/", "", $receiveddate));
    $receiveddate = strtotime(preg_replace("/\\(.*\\)/", "", $receiveddate));
    if ($receiveddate == false || $receiveddate == -1) {
        printf("cleanupDate() : Received date is false. Message might be broken.");
        return null;
    }
    return $receiveddate;
}