<?php
// sun_information.php
// RJM Programming
// January, 2018
// Thanks to http://php.net/manual/en/function.date-sun-info.php
$results="";

$yyyy = "yyyy";
$mnthis = "mnthis";
$dythis = "dythis";
$dateis = "2006-12-12";
$latis = "0.000000"; // 31.7667;
$longis = "0.000000"; //35.2333;
$tzis="GMT";
if (isset($_GET['timezone'])) {
$tzis=urldecode($_GET['timezone']);
} else if (isset($_POST['timezone'])) {
$tzis=urldecode($_POST['timezone']);
}
date_default_timezone_set($tzis);

if (isset($_GET['dmdates'])) {
  $dateis=urldecode($_GET['dmdates']);
  if (isset($_GET['latitude']) && isset($_GET['longitude'])) {
   $latis=urldecode($_GET['latitude']);
   $longis=urldecode($_GET['longitude']);
   $sun_info = date_sun_info(strtotime($dateis), $latis, $longis);
   $results.="<br>";
   foreach ($sun_info as $key => $val) {
     $results.="&nbsp;&nbsp;&nbsp;   $key: " . date("H:i:s", $val) . " " . $tzis . "<br>";
   }
   $results.="<br>";
  }
  $yyyy=explode("-", $dateis)[0];
  $mnthis=(-1 + explode("-", $dateis)[1]);
  $dythis=explode("-", $dateis)[2];
} else if (isset($_POST['dmdates'])) {
  $dateis=urldecode($_POST['dmdates']);
  if (isset($_POST['latitude']) && isset($_POST['longitude'])) {
   $latis=urldecode($_POST['latitude']);
   $longis=urldecode($_POST['longitude']);
   $sun_info = date_sun_info(strtotime($dateis), $latis, $longis);
   $results.="<br>";
   foreach ($sun_info as $key => $val) {
     $results.="&nbsp;&nbsp;&nbsp;   $key: " . date("H:i:s", $val) . " " . $tzis . "<br>";
   }
   $results.="<br>";
  }
  $yyyy=explode("-", $dateis)[0];
  $mnthis=(-1 + explode("-", $dateis)[1]);
  $dythis=explode("-", $dateis)[2];
}

echo "<!doctype><html><head>
      <title>Sun Information - RJM Programming - January, 2018 ... Thanks to http://php.net/manual/en/function.date-sun-info.php</title>
      <scrip" . "t type='text/javascript'>
           var today=new Date(); 
           var yyyy = today.getFullYear();
           var mnthis = today.getMonth();
           var dythis = today.getDate();
           var selc = 0;
           var geolocation = null;

function errorHandler(err) {
  if (err.code == 1) {
    if (1 == 2) alert('Access is denied');
  }

}

function showLocation( position ) {
  document.getElementById('latitude').value = '' + position.coords.latitude;
  document.getElementById('longitude').value = '' + position.coords.longitude;
}

function getLocation() {
  if (selc == 0) {
   selc=1;
   geolocation = navigator.geolocation;
   geolocation.getCurrentPosition(showLocation, errorHandler, {maximumAge: 75000});
  }
}
           function ol() {
           var tzis='' + Intl.DateTimeFormat().resolvedOptions().timeZone;   /" . "/ Thanks to https://stackoverflow.com/questions/22618056/javascript-timezone-information-how-to-get-america-los-angeles-or-equivalent/22625076#22625076 
           if (tzis != '') document.getElementById('timezone').value=tzis;
           document.getElementById('myform').action=document.URL.split('#')[0].split('?')[0];
           var dprefix='0', mprefix='0', i, months=['','January','February','March','April','May','June','July','August','September','October','November','December'];
           for (i=eval('' + yyyy) + 1; i>(eval('' + yyyy) - 100); i--) {
             if (i == " . $yyyy . ") {
             document.getElementById('dmdates').value='' + i;
             document.getElementById('syears').innerHTML+='<option value=' + i + ' selected>' + i + '</option>';
             } else {
             document.getElementById('syears').innerHTML+='<option value=' + i + '>' + i + '</option>';
             }
           } 
           for (i=1; i<=12; i++) {
             if (eval(-1 + i) == " . $mnthis . ") {
             document.getElementById('dmdates').value+='-' + mprefix + i;
             document.getElementById('smonths').innerHTML+='<option value=' + mprefix + i + ' selected>' + months[i] + '</option>';
             } else {
             document.getElementById('smonths').innerHTML+='<option value=' + mprefix + i + '>' + months[i] + '</option>';
             }
             if (i == 9) mprefix='';
           }
           for (i=1; i<=31; i++) {
             if (i == " . $dythis . ") {
             document.getElementById('dmdates').value+='-' + mprefix + i;
             document.getElementById('sdays').innerHTML+='<option value=' + dprefix + i + ' selected>' + dprefix + i + '</option>';
             } else {
             document.getElementById('sdays').innerHTML+='<option value=' + dprefix + i + '>' + dprefix + i + '</option>';
             }
             if (i == 9) dprefix='';
           }
           getLocation();
        }
        function dc(sois) {
           if (sois.value != '') {
           var paro=null, lastc=sois.id.slice(-1);
           if (sois.id.indexOf('year') != -1) {
             paro=document.getElementById(sois.id.replace('s','dm').replace('year','date'));
             if (paro.value == '') {
               if (document.getElementById(sois.id.replace('year' + lastc,'month' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('year' + lastc,'month' + lastc)).value='01';
               }
               if (document.getElementById(sois.id.replace('year' + lastc,'day' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('year' + lastc,'day' + lastc)).value='01';
               }
             }
             paro.value=sois.value + '-' + document.getElementById(sois.id.replace('year' + lastc,'month' + lastc)).value + '-' + document.getElementById(sois.id.replace('year' + lastc,'day' + lastc)).value;
           } else if (sois.id.indexOf('month') != -1) {
             paro=document.getElementById(sois.id.replace('s','dm').replace('month','date'));
             if (paro.value == '') {
               if (document.getElementById(sois.id.replace('month' + lastc,'day' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('month' + lastc,'day' + lastc)).value='01';
               }
               if (document.getElementById(sois.id.replace('month' + lastc,'year' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('month' + lastc,'year' + lastc)).value='' + yyyy;
               }
             }
             paro.value=document.getElementById(sois.id.replace('month' + lastc,'year' + lastc)).value + '-' + sois.value + '-' + document.getElementById(sois.id.replace('month' + lastc,'day' + lastc)).value;
           } else if (sois.id.indexOf('day') != -1) {
             paro=document.getElementById(sois.id.replace('s','dm').replace('day','date'));
             if (paro.value == '') {
               if (document.getElementById(sois.id.replace('day' + lastc,'month' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('day' + lastc,'month' + lastc)).value='01';
               }
               if (document.getElementById(sois.id.replace('day' + lastc,'year' + lastc)).value == '') {
                 document.getElementById(sois.id.replace('day' + lastc,'year' + lastc)).value='' + yyyy;
               }
             }
             paro.value=document.getElementById(sois.id.replace('day' + lastc,'year' + lastc)).value + '-' + document.getElementById(sois.id.replace('day' + lastc,'month' + lastc)).value + '-' + sois.value;
           }
           }
        }
        function checkthings() {
           return true;
        }
        window.onload=ol;
 </scri" . "pt></head>
<body> 
<h1>Sun Information</h1>
<h3>RJM Programming - January, 2018</h3>
<h3>Thanks to <a target=_blank title='http://php.net/manual/en/function.date-sun-info.php' href='http://php.net/manual/en/function.date-sun-info.php'>http://php.net/manual/en/function.date-sun-info.php</a></h3>
<div id=results style='background-color:pink;'>" . $results . "</div>
<br>
<form style='padding: 5px 5px 5px 5px;border:3px solid green;background-color:lightgray;' id=myform onsubmit='return checkthings();' action=./sun_information.php method=POST>
Date: <input type=hidden name=dmdates id=dmdates value=''></input><select style=display:inline-flex; onchange=dc(this); id=syears><option value=''></option></select><select style=display:inline-flex; onchange=dc(this); id=smonths><option value=''></option></select><select style=display:inline-flex; onchange=dc(this); id=sdays><option value=''></option></select>  Timezone: <input type=text id=timezone name=timezone value='" . $tzis . "'></input>  Latitude: <input name='latitude' id='latitude' type='number' step='0.000001' value='" . $latis . "' min='-90.000000' max='90.000000'></input>  Longitude: <input name='longitude' id='longitude' type='number' step='0.000001' value='" . $longis . "' min='-180.000000' max='180.000000'></input><br><br>
<input type=submit value='Sun Information' style='background-color:yellow;'></input>
</body></html>";
?>
