function getUSNOsunmoon() { global $SITE, $myLong, $myLat, $sunMoonInfo; $Z = round(date(" Z", time()) / 3600); # $mdy = date('n/j/Y', time()); $USNOUrl = "http://api.usno.navy.mil/rstt/oneday?date={$mdy}&coords={$myLat},{$myLong}&tz={$Z}"; #http://199.211.133.93 $USNOUrl = "http://199.211.133.93/rstt/oneday?date={$mdy}&coords={$myLat},{$myLong}&tz={$Z}"; $html = '{ "error":false, "apiversion":"1.0", "year":2015, "month":7,"day":30, "datechanged":false, "lon":-72.79,"lat":41.30, "tz":-4, "sundata":[ {"phen":"BC", "time":"05:13"}, {"phen":"R", "time":"05:44"}, {"phen":"U", "time":"12:58"}, {"phen":"S", "time":"20:10"}, {"phen":"EC", "time":"20:42"} ], "moondata":[ {"phen":"S", "time":"04:51"}, {"phen":"R", "time":"19:25"} ] , "prevmoondata": [ {"phen":"R","time":"18:35"} , {"phen":"U","time":"23:42"} ], "closestphase":{ "phase":"Full Moon", "date":"July 31, 2015","time":"06:43"}, "fracillum":"99%" , "curphase":"Waxing Gibbous" }'; $test = false; if (!$test) { $html = usno_makeRequest($USNOUrl, ''); // load sun moon data from usno site } # $MoonJSON = json_decode($html, true); if (!is_array($MoonJSON)) { $sunMoonInfo['error'] = 'invalid data - no array'; return false; } elseif (!isset($MoonJSON['error'])) { $sunMoonInfo['error'] = 'invalid data - missing error field'; } elseif ($MoonJSON['error'] != false) { $sunMoonInfo['error'] = $MoonJSON['error']; } # $dateprior = date('Y/m/d', strtotime("-1 day")); $datenow = date('Y/m/d'); $datenext = date('Y/m/d', strtotime("+1 day")); $from = strtotime($datenow . ' 00:00'); $to = strtotime($datenext . ' 00:00'); $sunMoonInfo['beginciviltwilight'] = '$from'; $sunMoonInfo['sunrise'] = '$from'; $sunMoonInfo['suntransit'] = strtotime($datenow . ' 12:00'); $sunMoonInfo['sunset'] = '$to'; $sunMoonInfo['endciviltwilight'] = '$to'; $suntrans['BC'] = 'beginciviltwilight'; $suntrans['R'] = 'sunrise'; $suntrans['U'] = 'suntransit'; $suntrans['S'] = 'sunset'; $suntrans['EC'] = 'endciviltwilight'; $count = count($MoonJSON['sundata']); for ($n1 = 0; $n1 < $count; $n1++) { $arr = $MoonJSON['sundata'][$n1]; $key = $arr['phen']; $time = $arr['time']; $unix = strtotime($datenow . ' ' . $time); $new_key = $suntrans[$key]; $sunMoonInfo[$new_key] = $unix; } $moontrans['R'] = 'moonrise'; $moontrans['U'] = 'moonransit'; $moontrans['S'] = 'moonset'; $count = count($MoonJSON['moondata']); for ($n1 = 0; $n1 < $count; $n1++) { $arr = $MoonJSON['moondata'][$n1]; $key = $arr['phen']; $time = $arr['time']; $unix = strtotime($datenow . ' ' . $time); $new_key = $moontrans[$key]; $sunMoonInfo[$new_key] = $unix; } if (isset($MoonJSON['prevmoondata'])) { $count = count($MoonJSON['prevmoondata']); for ($n1 = 0; $n1 < $count; $n1++) { $arr = $MoonJSON['prevmoondata'][$n1]; $key = $arr['phen']; $time = $arr['time']; $unix = strtotime($dateprior . ' ' . $time); $new_key = $moontrans[$key]; if (isset($sunMoonInfo[$new_key])) { $new_key = 'prev_' . $new_key; } $sunMoonInfo[$new_key] = $unix; } } if (isset($MoonJSON['nextmoondata'])) { $count = count($MoonJSON['nextmoondata']); for ($n1 = 0; $n1 < $count; $n1++) { $arr = $MoonJSON['nextmoondata'][$n1]; $key = $arr['phen']; $time = $arr['time']; $unix = strtotime($datenext . ' ' . $time); $new_key = $moontrans[$key]; if (isset($sunMoonInfo[$new_key])) { $new_key = 'next_' . $new_key; } $sunMoonInfo[$new_key] = $unix; } } if (isset($MoonJSON['curphase'])) { $sunMoonInfo['moonPhase'] = $MoonJSON['curphase']; } elseif (isset($MoonJSON['closestphase']['phase'])) { $sunMoonInfo['moonPhase'] = $MoonJSON['closestphase']['phase']; $phase = strtolower(str_replace(' ', '', $sunMoonInfo['moonPhase'])); switch ($phase) { case 'newmoon': $sunMoonInfo['moonPerc'] = '0%'; break; case 'fullmoon': $sunMoonInfo['moonPerc'] = '100%'; break; default: $sunMoonInfo['moonPerc'] = '50%'; break; } // eo switch } if (isset($MoonJSON['fracillum'])) { $sunMoonInfo['moonPerc'] = $MoonJSON['fracillum']; } if (isset($sunMoonInfo['sunrise']) and isset($sunMoonInfo['sunset'])) { $diff = $sunMoonInfo['sunset'] - $sunMoonInfo['sunrise']; $diffh = intval($diff / 3600); // hours $diffm = intval($diff / 60 % 60); $sunMoonInfo['hoursofpossibledaylight'] = sprintf("%02d:%02d", $diffh, $diffm); } #echo '<pre>'.print_r($sunMoonInfo,true); exit; return; }
function getUSNOsunmoon() { global $SITE, $myLong, $myLat, $sunMoonInfo; $Z = round(date(" Z", time()) / 3600); # $USNOUrl = "http://api.usno.navy.mil/rstt/oneday?date=today&coords={$myLat},{$myLong}&tz={$Z}"; /*What we need is: $sunMoonInfo => [date] => 1438186636 [error] => [beginciviltwilight] => 1438163580 [sunrise] => 1438165440 [suntransit] => 1438191240 [sunset] => 1438217040 [endciviltwilight] => 1438218900 [moonrise] => 1438211340 [moontransit] => 1438230000 [moonset] => 1438248780 [prev_moonrise] => 1438191840 optional [prev_moontransit] => 1438230000 optional [prev_moonset] => 1438191840 optional [next_moonrise] => 1438191840 optional [next_moontransit] => 1438230000 optional [next_moonset] => 1438191840 optional [moonPhase] => waxing gibbous [moonPerc] => 96 [hoursofpossibledaylight] => 14:20 */ # USNO returns info like we use for test where the codes are: # BC = Begin civil twilight # R = Rise # U = Upper Transit # S = Set # EC = End civil twilight $html = '{ "error":false, "apiversion":"1.0", "year":2015, "month":7,"day":30, "datechanged":false, "lon":-72.79,"lat":41.30, "tz":-4, "sundata":[ {"phen":"BC", "time":"05:13"}, {"phen":"R", "time":"05:44"}, {"phen":"U", "time":"12:58"}, {"phen":"S", "time":"20:10"}, {"phen":"EC", "time":"20:42"} ], "moondata":[ {"phen":"S", "time":"04:51"}, {"phen":"R", "time":"19:25"} ] , "prevmoondata": [ {"phen":"R","time":"18:35"} , {"phen":"U","time":"23:42"} ], "closestphase":{ "phase":"Full Moon", "date":"July 31, 2015","time":"06:43"}, "fracillum":"99%" , "curphase":"Waxing Gibbous" }'; $test = false; if (!$test) { $html = usno_makeRequest($USNOUrl, ''); // load sun moon data from usno site } # $MoonJSON = json_decode($html, true); if (!is_array($MoonJSON)) { $sunMoonInfo['error'] = 'invalid data - no array'; return false; } elseif (!isset($MoonJSON['error'])) { $sunMoonInfo['error'] = 'invalid data - missing error field'; } elseif ($MoonJSON['error'] != false) { $sunMoonInfo['error'] = $MoonJSON['error']; } # $dateprior = date('Y/m/d', strtotime("-1 day")); $datenow = date('Y/m/d'); $datenext = date('Y/m/d', strtotime("+1 day")); $from = strtotime($datenow . ' 00:00'); $to = strtotime($datenext . ' 00:00'); $sunMoonInfo['beginciviltwilight'] = '$from'; $sunMoonInfo['sunrise'] = '$from'; $sunMoonInfo['suntransit'] = strtotime($datenow . ' 12:00'); $sunMoonInfo['sunset'] = '$to'; $sunMoonInfo['endciviltwilight'] = '$to'; $suntrans['BC'] = 'beginciviltwilight'; $suntrans['R'] = 'sunrise'; $suntrans['U'] = 'suntransit'; $suntrans['S'] = 'sunset'; $suntrans['EC'] = 'endciviltwilight'; $count = count($MoonJSON['sundata']); for ($n1 = 0; $n1 < $count; $n1++) { $arr = $MoonJSON['sundata'][$n1]; $key = $arr['phen']; $time = $arr['time']; $unix = strtotime($datenow . ' ' . $time); $new_key = $suntrans[$key]; $sunMoonInfo[$new_key] = $unix; } $moontrans['R'] = 'moonrise'; $moontrans['U'] = 'moonransit'; $moontrans['S'] = 'moonset'; $count = count($MoonJSON['moondata']); for ($n1 = 0; $n1 < $count; $n1++) { $arr = $MoonJSON['moondata'][$n1]; $key = $arr['phen']; $time = $arr['time']; $unix = strtotime($datenow . ' ' . $time); $new_key = $moontrans[$key]; $sunMoonInfo[$new_key] = $unix; } if (isset($MoonJSON['prevmoondata'])) { # usno_traverse ($MoonJSON['prevmoondata'], $dateprior); $count = count($MoonJSON['prevmoondata']); for ($n1 = 0; $n1 < $count; $n1++) { $arr = $MoonJSON['prevmoondata'][$n1]; $key = $arr['phen']; $time = $arr['time']; $unix = strtotime($dateprior . ' ' . $time); $new_key = $moontrans[$key]; if (isset($sunMoonInfo[$new_key])) { $new_key = 'prev_' . $new_key; } $sunMoonInfo[$new_key] = $unix; } } if (isset($MoonJSON['nextmoondata'])) { # usno_traverse ($MoonJSON['nextmoondata'], $datenext); $count = count($MoonJSON['nextmoondata']); for ($n1 = 0; $n1 < $count; $n1++) { $arr = $MoonJSON['nextmoondata'][$n1]; $key = $arr['phen']; $time = $arr['time']; $unix = strtotime($datenext . ' ' . $time); $new_key = $moontrans[$key]; if (isset($sunMoonInfo[$new_key])) { $new_key = 'next_' . $new_key; } $sunMoonInfo[$new_key] = $unix; } } if (isset($MoonJSON['curphase'])) { $sunMoonInfo['moonPhase'] = $MoonJSON['curphase']; } elseif (isset($MoonJSON['closestphase']['phase'])) { $sunMoonInfo['moonPhase'] = $MoonJSON['closestphase']['phase']; $phase = strtolower(str_replace(' ', '', $sunMoonInfo['moonPhase'])); switch ($phase) { case 'newmoon': $sunMoonInfo['moonPerc'] = '0%'; break; case 'fullmoon': $sunMoonInfo['moonPerc'] = '100%'; break; default: $sunMoonInfo['moonPerc'] = '50%'; break; } } if (isset($MoonJSON['fracillum'])) { $sunMoonInfo['moonPerc'] = $MoonJSON['fracillum']; } if (isset($sunMoonInfo['sunrise']) and isset($sunMoonInfo['sunset'])) { $diff = $sunMoonInfo['sunset'] - $sunMoonInfo['sunrise']; $diffh = intval($diff / 3600); // hours $diffm = intval($diff / 60 % 60); $sunMoonInfo['hoursofpossibledaylight'] = sprintf("%02d:%02d", $diffh, $diffm); } #echo '<pre>'.print_r($sunMoonInfo,true); exit; return; }