I have Latitueds and longitudes for 744 United States cities. I am wanting to provide a small black that accesses these to produce the sunrise and sunset for these cities.

When I use the form as shown below it works for my local time but when entering values for California (offset 7 hours) it produces 28:13 for sunrise. here is the sample code.

<?php
$city = "Los Angeles";
$state = "California";
$lat = 34.3;
$long = 118.4;
echo"The sunrise for ".$city." ".$state."  is<br>";
echo date("D M d Y"). ', sunrise time : ' .date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long,90,7);
echo"<br><br>";
echo"And the suset is<br>";
echo date("D M d Y"). ', sunset time :  ' .date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, 90, 7);

How do I make this work with out weird times showing up and how should I make it show tomorrows data instead of todays?

    Im not a good navigator, but have you take this points into count?

    The latitude defaults to North. So, if you want to specify a South value, you must pass a negative value. The same note applies to longitude, which defaults to East.

    date_sunrise
    date_sunset

      That makes it worse, especially if you use a negative offset the longitude is in fact west but the latitude is North so it wouldn't apply.

        Right; I never thought the United States was in the Southern Hemisphere (nor in the Eastern).

        Try returning a timestamp, and then using date() to reformat it. I don't know if the GMT offset is then required (since the timestamp is defined in terms of GMT anyway).

        But it's a silly return value; it's probably supposed to be 04:13 tomorrow or something. Have a look through bugs.php.net for date_sunrise issues.

          Generally I use Date("U") and then do all of my conversions to that or whatever and then echo out the date like this:

          <?php
          $dateNow = Date("U"); // Date in unix format
          $dateAfterConv = $dateNow + (60602); // Adds two hours to the current date
          echo Date("D M d Y", $dateAfterConv); // Echo's out the correctly formatted date from the date we converted above
          ?>

            Thanks, right now I'll try just about anything after building the database for it, I'd hate to think that I wasted all that time entering all that data and not be able to use it because it come up with silly values that don't make sense.

              What I was looking for (kind of).

              The sunrise and sunset for Los Angeles California is
              Sun Oct 09 2005 sunrise time: 06:57
              and the sunset for tomorrow is:
              Sun Oct 09 2005 sunset time: 18:38

              This is the wierd code I ended up using and will test on other lat-longs

              <?php
              $city = "Los Angeles";
              $state = "California";
              $lat = 34.3;
              $long = -118.4;
              $dateNow = Date("U"); // Date in unix format
              $dateFixed = $dateNow + (16*60*60); // Adds 16 hours to the current date
              echo "The sunrise and sunset for $city $state is<br>";
              echo Date("D M d Y", $dateFixed),' sunrise time: ' .date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long,90,-7);
              echo"<br>and the sunset for tomorrow is:<br>";
              echo Date("D M d Y", $dateFixed),' sunset time: ' .date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long*(-1), 90, 9);
              ?>

              It would seem that there is definately some notice that for the date_sunset() I actually used positive values (negatives did not work) to get a real close to the actual table I saw on a web site that had these values posted. The offset was changed to 9 (PST is GMT-8) so there is something really wrong here!

              Any other ideas?

                Negative numbers wont work at all?

                Like if you were to times the number by say -2 or something it would mess what your trying to do up?

                  Have you tried getting date_sunrise to return a timestamp? (Incidentally, [man]date/man is exactly equivalent to [man]time/man; so replacing one with the other wouldn't make any difference.)

                    What I ended up using works to a degree, the sunset function seems to be the biggest problem. Although I checked with published sites that post these things as either a table or links to data they have been compiled by those in the industry of timekeeping, latitude longitude and elevation and all that sort of thing. The function takes lat/long zenith (usually 90 degrees) GMT or UTC offset and has a default for all time functions as below:

                    date.default_latitude "31.7667" PHP_INI_ALL Available since PHP 5.0.0.
                    date.default_longitude "35.2333" PHP_INI_ALL Available since PHP 5.0.0.
                    date.sunrise_zenith "90.83" PHP_INI_ALL Available since PHP 5.0.0.
                    date.sunset_zenith "90.83" PHP_INI_ALL Available since PHP 5.0.0.
                    date.timezone "" PHP_INI_ALL Available since PHP 5.1.0.

                    For the sunrise function here is the detail:

                    Description
                    mixed date_sunrise ( int timestamp [, int format [, float latitude [, float longitude [, float zenith [, float gmt_offset]]]]] )

                    date_sunrise() returns the sunrise time for a given day (specified as a timestamp) and location. The latitude, longitude and zenith parameters default to the date.default_latitude, date.default_longitude and date.sunrise_zenith configuration options, respectively.

                    The latitude defaults to North. So, if you want to specify a South value, you must pass a negative value. The same note applies to longitude, which defaults to East.

                    The gmt_offset parameter is specified in hours.

                    Table 1. format constants

                    constant description example
                    SUNFUNCS_RET_STRING returns the result as string 16:46
                    SUNFUNCS_RET_DOUBLE returns the result as float 16.78243132
                    SUNFUNCS_RET_TIMESTAMP returns the result as integer (timestamp) 1095034606

                    So Los Angeles California has a latitude of 34.3 N (not negative), with a longitude of 118.4 W (negative from default), and a GMT or UTC offset of 7 hours.
                    I might try using the zenith of 90.83 instead of 90, right now I am off by a douple of minutes on sunrise but up to an hour including daylight savings time which I test for as an option...not gonna test for the new Federal Law about what dates and all the changes go into effect and where, just that Arizona doesn't use it at all.

                    So with all that said here is how I got close with this problem but still perhaps not quite right.

                    city = $local;
                    $state = $stval;
                    //echo "$city,$state";
                    $now = date("g:i:A");
                    $host ="localhost";
                    $pass = "";
                    $database = "sunrise";
                    $user ="root";
                    $connection = mysql_connect($host,$user,$pass)
                        or die ("Could not connect to server!");
                    $db = mysql_select_db($database,$connection)
                        or die ("Could not select $database");
                    $query = "SELECT city,state,lat,lng,off FROM sunrise WHERE city = '$city' and state= '$state'";
                    $result = mysql_query($query)
                        or die ("Could not execute database query!");
                    while ($row = mysql_fetch_array($result)){
                      extract($row);
                      echo "<b>$city, $state</b> latitude $lat, longitude $lng, GMT Offset $off.<br><br>";
                    }
                    $lng =$lng*(-1);
                    if ($state =="Arizona"){
                      $off = $off+(1);
                    }
                    echo "Offset =";
                    echo "$off";
                    echo "<br>";
                    echo "The time is now ";
                    echo $now;
                    echo "<br>";
                    $dateNow = Date("U"); // Date in unix format
                    $dateFixed = $dateNow + (16*60*60); // Adds 16 hours to the current date
                    echo "The sunrise for $city $state is<br>";
                    echo Date("D M d Y", $dateFixed),' sunrise time: ' .date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $lng,90.83,$off*(-1));
                    echo"<br><br>and the sunset for tomorrow is:<br>";
                    echo Date("D M d Y", $dateFixed),' sunset time: ' .date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $lng*(-1), 90.83, $off);

                    Keep in mind this is for DST and not ST! The reason for checking for state of Arizona and subtracting one hour from the offset.

                      Write a Reply...