PHP5 Sunrise and Sunset functionality

Before I re-invent the wheel, I was wondering if anyone had more code involving PHP5’s date_sunrise and date_sunset.

This is what I currently have from searching the Internet…

<?php

// Your location
$lat = 41.290077;    // South is negative
$long = -95.916189;    // West is negative
$offset = -5;    // difference between GMT and local time in hours

$zenith=90+50/60;
echo "
<p>Sunrise: ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
echo "
Sunset: ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);

$zenith=96;
echo "
<p>Civilian Twilight start: ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
echo "
Civilian Twilight end: ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);

$zenith=102;
echo "
<p>Nautical Twilight start: ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
echo "
Nautical Twilight end: ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);

$zenith=108;
echo "
<p>Astronomical Twilight start: ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
echo "
Astronomical Twilight end: ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);

?>

I was thinking there must be code for:

Length Of Visible Light
Length of day
Tomorrow’s difference (Day will be X shorter; or Day will be X longer)

Anybody already do the code for those?

Rather than doing the offset, I wonder if it would be easier to assign the timezone with a TZ.

With regards to timezones…

In PHP 5 >= 5.1.0 to get proper time for your locale, you are supposed to use the function date_default_timezone_set … which is the “new” way of setting the TZ rather than using putenv(“TZ=…”)… This is to get around the problem when the server is restricting the user from using putenv…

If that is set, you won’t need an offset unless you are attempting to get a result outside of your locale.

You will also get warnings if the timezone by default is not set with a valid timezone…

Ref:
http://us3.php.net/manual/en/function.date-default-timezone-set.php

Code below will set the TZ using the what should be available…


// SET TIMEZONE

$TZ="US/Central";    # Or the more modern way  $TZ="America/Chicago";
if (phpversion() >= "5.1.0") {
    date_default_timezone_set($TZ);
} else {
    putenv("TZ=" . $TZ);
}

BTW… someone else was playing with this stuff. I remember because it uncovered a bug in the version of PHP I was using.

Found one of them:
http://discourse.weather-watch.com/t/32953

Thanks Kevin. What was the bug exactly? With setting the time?

I was bored this morning waiting for sunrise, so I played some more…

http://www.carterlake.org/test4.php

<?php

// Your location
$lat = 41.290077;    // South is negative
$long = -95.916189;    // West is negative
$offset = -5;    // difference between GMT and local time in hours

$zenith=90+50/60;
echo "Sunrise: ".date("g:ia",strtotime(date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset)));
echo "
Sunset: ".date("g:ia",strtotime(date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset)));

$zenith=96;
echo "

Civil Twilight start: ".date("g:ia",strtotime(date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset)));
echo "
Civil Twilight end: ".date("g:ia",strtotime(date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset)));

$zenith=102;
echo "

Nautical Twilight start: ".date("g:ia",strtotime(date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset)));
echo "
Nautical Twilight end: ".date("g:ia",strtotime(date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset)));

$zenith=108;
echo "

Astronomical Twilight start: ".date("g:ia",strtotime(date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset)));
echo "
Astronomical Twilight end: ".date("g:ia",strtotime(date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset)));

$zenith=90+50/60;
$lengthofday = date_sunset(time(), SUNFUNCS_RET_DOUBLE, $lat, $long, $zenith, $offset) - date_sunrise(time(), SUNFUNCS_RET_DOUBLE, $lat, $long, $zenith, $offset);

$lengthofday = $lengthofday * 60 * 60;

echo "


";

echo "Length of Day: " . secs_to_string($lengthofday);

$zenith=96;
$lengthofday = date_sunset(time(), SUNFUNCS_RET_DOUBLE, $lat, $long, $zenith, $offset) - date_sunrise(time(), SUNFUNCS_RET_DOUBLE, $lat, $long, $zenith, $offset);

$lengthofday = $lengthofday * 60 * 60;

echo "
";

echo "Length of Visible Light: " . secs_to_string($lengthofday);

$zenith=90+50/60;
$lengthofday = (date_sunset(time()+86400, SUNFUNCS_RET_DOUBLE, $lat, $long, $zenith, $offset) - date_sunrise(time()+86400, SUNFUNCS_RET_DOUBLE, $lat, $long, $zenith, $offset))-(date_sunset(time(), SUNFUNCS_RET_DOUBLE, $lat, $long, $zenith, $offset) - date_sunrise(time(), SUNFUNCS_RET_DOUBLE, $lat, $long, $zenith, $offset));

$lengthofday = $lengthofday * 60 * 60;

echo "
";

echo "Tomorrow will be: " . secs_to_string2 ($lengthofday);


function secs_to_string ($secs)
{
  // reset hours, mins, and secs we'll be using
  $hours=0;
  $mins = 0;
  $secs = intval ($secs);
  $t = array(); // hold all 3 time periods to return as string
  
  // take care of mins and left-over secs
  if ($secs >= 60) {
    $mins += (int) floor ($secs / 60);
    $secs = (int) $secs % 60;
        
    // now handle hours and left-over mins    
    if ($mins >= 60) {
      $hours += (int) floor ($mins / 60);
      $mins = $mins % 60;
    }
    // we're done! now save time periods into our array
    $t['hours'] = (intval($hours) < 10) ? "0" . $hours : $hours;
    $t['mins'] = (intval($mins) < 10) ? "0" . $mins : $mins;
  }

  // build the pretty time string in an ugly way
  $time_string = "";
  $time_string .= $t['hours'] . "h ";
  $time_string .= $t['mins'] . "m";

  return empty($time_string) ? 0 : $time_string;
}

function secs_to_string2 ($secs)
{
  // mins, and secs we'll be using
  $mins = 0;
  $secs = intval ($secs);

  if ($secs < 0) {

   $dayis = "shorter"; 
   $secs = abs ($secs);

    } else {

   $dayis = "longer"; }
  
  // take care of mins and left-over secs
  if ($secs >= 60) {
    $mins += (int) floor ($secs / 60);
    $secs = (int) $secs % 60;
        
  }

  // build the time string
  $time_string = "";
  $time_string .= $mins . "m ";
  $time_string .= $secs . "s " . $dayis;

  return empty($time_string) ? 0 : $time_string;
}

?>

I’m even more bored and tired… got paged on a down server at 4:30am and am still “watching” it.

The bug with with the date_sunrise function and the version I was using…

See how bored… a little code (functionized)

<?php
// set elsewhere
$SITE['TZ'] = "America/Phoenix";
$SITE['LAT'] = 33.4026;
$SITE['LONG'] = -111.8831;

get_times();

echo "
\nToday:
\n";
echo "Sunrise: " . $VALS['sunrise'] . "
\n";
echo "Sunset: " . $VALS['sunset'] . "
\n";
echo "Civilian Twilight start: " . $VALS['civiltwstart'] . "
\n";
echo "Civilian Twilight end: " . $VALS['civiltwend'] . "
\n";
echo "Nautical Twilight start: " . $VALS['nauticaltwstart'] . "
\n";
echo "Nautical Twilight end: " . $VALS['nauticaltwend'] . "
\n";
echo "Astronomical Twilight start: " . $VALS['astrotwstart'] . "
\n";
echo "Astronomical Twilight end: " . $VALS['astrotwend'] . "
\n";
echo "Length of Visible Light: " . $VALS['lenvislight'] . "
\n";
echo "Length of Day: " . $VALS['lenofday'] . "
\n";

echo "
\nTommorrow:
\n";

echo "Sunrise: " . $VALS['tsunrise'] . "
\n";
echo "Sunset: " . $VALS['tsunset'] . "
\n";
echo "Civilian Twilight start: " . $VALS['tciviltwstart'] . "
\n";
echo "Civilian Twilight end: " . $VALS['tciviltwend'] . "
\n";
echo "Nautical Twilight start: " . $VALS['tnauticaltwstart'] . "
\n";
echo "Nautical Twilight end: " . $VALS['tnauticaltwend'] . "
\n";
echo "Astronomical Twilight start: " . $VALS['tastrotwstart'] . "
\n";
echo "Astronomical Twilight end: " . $VALS['tastrotwend'] . "
\n";
echo "Length of Visible Light: " . $VALS['tlenvislight'] . "
\n";
echo "Length of Day: " . $VALS['tlenofday'] . "
\n";

echo "
\n";

echo "Diff Visable Light from Yesterday: " . 
    intval (date("i", (getTimestamp($VALS['lenvislight']) - getTimestamp($VALS['tlenvislight'])))) .
    "
\n";
echo "Diff Length of day from Yesterday: " . 
    intval (date("i", (getTimestamp($VALS['lenofday']) - getTimestamp($VALS['tlenofday'])))) .
    "
\n";

    
exit;

function get_times() {
    global $SITE, $VALS;
    
    // Set the timezone we want to use
    set_tz( $SITE['TZ'] );
    
    // Use your location information
    $lat = $SITE['LAT'];    // South is negative
    $long = $SITE['LONG'];   // West is negative
    $timeused = $timetoday;
    $timetoday = time();
    $timetommorrow = $timetoday + 86400;
    
    $zenith=90+50/60;
    $VALS['sunrise'] = date_sunrise($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['sunset'] = date_sunset($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tsunrise'] = date_sunrise($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tsunset'] = date_sunset($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
        
    $zenith=96;
    $VALS['civiltwstart'] = date_sunrise($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['civiltwend'] = date_sunset($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tciviltwstart'] = date_sunrise($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tciviltwend'] = date_sunset($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
        
    $zenith=102;
    $VALS['nauticaltwstart'] = date_sunrise($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['nauticaltwend'] = date_sunset($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tnauticaltwstart'] = date_sunrise($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tnauticaltwend'] = date_sunset($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
        
    $zenith=108;
    $VALS['astrotwstart'] = date_sunrise($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['astrotwend'] = date_sunset($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tastrotwstart'] = date_sunrise($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tastrotwend'] = date_sunset($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
        
    // Other values
    $VALS['lenvislight'] = date("H:i", (getTimestamp($VALS['civiltwend']) - getTimestamp($VALS['civiltwstart'])) );
    $VALS['lenofday'] = date("H:i", (getTimestamp($VALS['sunset']) - getTimestamp($VALS['sunrise'])) );
    $VALS['tlenvislight'] = date("H:i", (getTimestamp($VALS['tciviltwend']) - getTimestamp($VALS['tciviltwstart'])) );
    $VALS['tlenofday'] = date("H:i", (getTimestamp($VALS['tsunset']) - getTimestamp($VALS['tsunrise'])) );  

}



function set_tz ($TZ){
    if (phpversion() >= "5.1.0") {
        date_default_timezone_set($TZ);
    } else {
        putenv("TZ=" . $TZ);
    }
}

function getTimestamp ($val) {
    list ($hour,$min) = preg_split("/:/",$val);
    $ttl = ($min * 60) + ($hour * 60 * 60);
    return($ttl);
}

Sample output:

`Today:
Sunrise: 05:55
Sunset: 19:04
Civilian Twilight start: 05:30
Civilian Twilight end: 19:28
Nautical Twilight start: 05:00
Nautical Twilight end: 19:59
Astronomical Twilight start: 04:28
Astronomical Twilight end: 20:30
Length of Visible Light: 06:58
Length of Day: 06:09

Tommorrow:
Sunrise: 05:56
Sunset: 19:02
Civilian Twilight start: 05:31
Civilian Twilight end: 19:27
Nautical Twilight start: 05:01
Nautical Twilight end: 19:57
Astronomical Twilight start: 04:29
Astronomical Twilight end: 20:29
Length of Visible Light: 06:56
Length of Day: 06:06

Diff Visable Light from Yesterday: 2
Diff Length of day from Yesterday: 3`

Think I got the diff between days backward.

changed that code to:

echo "Diff Visable Light from Yesterday: " . 
    intval ((getTimestamp($VALS['tlenvislight']) - getTimestamp($VALS['lenvislight'])) / 60  ) .
    "
\n";
echo "Diff Length of day from Yesterday: " . 
    intval ( (getTimestamp($VALS['tlenofday']) - getTimestamp($VALS['lenofday'])) / 60   ) .
    "
\n";

So output is now:

`Today:
Sunrise: 05:55
Sunset: 19:04
Civilian Twilight start: 05:30
Civilian Twilight end: 19:28
Nautical Twilight start: 05:00
Nautical Twilight end: 19:59
Astronomical Twilight start: 04:28
Astronomical Twilight end: 20:30
Length of Visible Light: 06:58
Length of Day: 06:09

Tommorrow:
Sunrise: 05:56
Sunset: 19:02
Civilian Twilight start: 05:31
Civilian Twilight end: 19:27
Nautical Twilight start: 05:01
Nautical Twilight end: 19:57
Astronomical Twilight start: 04:29
Astronomical Twilight end: 20:29
Length of Visible Light: 06:56
Length of Day: 06:06

Diff Visable Light from Yesterday: -2
Diff Length of day from Yesterday: -3`

The length of day is the total time the sun is above the horizon . Not between sunrise and sunset.
http://www.srh.noaa.gov/ffc/html/gloss3.shtml

Visible light/length of day isn’t working correctly, should be about 14 hours, not 6.

Sunrise: 5:55am
Sunset: 7:04pm

Civil Twilight start: 5:30am
Civil Twilight end: 7:28pm

Nautical Twilight start: 5:00am
Nautical Twilight end: 7:59pm

Astronomical Twilight start: 4:28am
Astronomical Twilight end: 8:31pm

Length of Day: 13h 09m
Length of Visible Light: 13h 58m
Tomorrow will be: 1m 55s shorter

(Kevin - I think your code looks cleaner but you didn’t put as much effort into formatting :wink: )

I was looking at:

http://wiki.wunderground.com/index.php/Educational_-_Twilight

Length Of Visible Light This is defined to be the time of Civil Sunset minus the time of Civil Sunrise.

The use of the date function is wrong is wrong… what cheating on the output…

I will look at it when I get up… hitting the sack …

I have a function that I created that converts time to different outputs that I would use, but couldn’t put my hands on right off the bat.

I just tried it and it seems to be working for me. Length of day and visible light. Haven’t checked the rest.

–Dave

Today:
Sunrise: 10:27
Sunset: 23:49
Civilian Twilight start: 10:01
Civilian Twilight end: 00:16
Nautical Twilight start: 09:28
Nautical Twilight end: 00:49
Astronomical Twilight start: 08:54
Astronomical Twilight end: 01:23
Length of Visible Light: 14:15
Length of Day: 13:22

Tommorrow:
Sunrise: 10:28
Sunset: 23:48
Civilian Twilight start: 10:02
Civilian Twilight end: 00:14
Nautical Twilight start: 09:29
Nautical Twilight end: 00:47
Astronomical Twilight start: 08:55
Astronomical Twilight end: 01:21
Length of Visible Light: 14:12
Length of Day: 13:20

Diff Visable Light from Yesterday: 3
Diff Length of day from Yesterday: 2

Well, whoever created the functions should have given us the ability to format the output. Then we wouldn’t have to massage it further.

Guess mine isn’t too good. Sunrise around 10 am? I just guessed the time zone was America/New York which must not be the correct input.

–Dave

I was looking at the numbers in Kevin’s post:

Sunrise: 05:55
Sunset: 19:04
Civilian Twilight start: 05:30
Civilian Twilight end: 19:28
Nautical Twilight start: 05:00
Nautical Twilight end: 19:59
Astronomical Twilight start: 04:28
Astronomical Twilight end: 20:30
Length of Visible Light: 06:58
Length of Day: 06:09

Try:

America/New_York

What offset do you have?

The New_York worked but don’t see a place for an off-set. The time is off by one hour too early.

–Dave

You’re right, no offset, I was looking at Tom’s original code. Hmmm, DST I guess…

In the timezones you can use the newer versions “America/New_York”, “America/Phoenix”, or the older versions “US/Eastern”, “US/Pacific”, “US/Hawaii” …

Note that America is the continent, not the country USA… so Hawaii which is not on that continent but rather in the pacific, would be “Pacific/Honolulu”.

I changed the code a bit (added an out_time function I was playing with, and hacked it too)

New output

24 Hour Mode
`Today:
Sunrise: 05:55
Sunset: 19:04
Civilian Twilight start: 05:30
Civilian Twilight end: 19:28
Nautical Twilight start: 05:00
Nautical Twilight end: 19:59
Astronomical Twilight start: 04:28
Astronomical Twilight end: 20:30
Length of Visible Light: 13:58
Length of Day: 13:09

Tommorrow:
Sunrise: 05:56
Sunset: 19:02
Civilian Twilight start: 05:31
Civilian Twilight end: 19:27
Nautical Twilight start: 05:01
Nautical Twilight end: 19:57
Astronomical Twilight start: 04:29
Astronomical Twilight end: 20:29
Length of Visible Light: 13:56
Length of Day: 13:06

Diff Visable Light from Yesterday: -2 mins
Diff Length of day from Yesterday: -3 mins`


12 hour Mode

`Today:
Sunrise: 5:55 am
Sunset: 7:04 pm
Civilian Twilight start: 5:30 am
Civilian Twilight end: 7:28 pm
Nautical Twilight start: 5:00 am
Nautical Twilight end: 7:59 pm
Astronomical Twilight start: 4:28 am
Astronomical Twilight end: 8:30 pm
Length of Visible Light: 1:58 pm
Length of Day: 1:09 pm

Tommorrow:
Sunrise: 5:56 am
Sunset: 7:02 pm
Civilian Twilight start: 5:31 am
Civilian Twilight end: 7:27 pm
Nautical Twilight start: 5:01 am
Nautical Twilight end: 7:57 pm
Astronomical Twilight start: 4:29 am
Astronomical Twilight end: 8:29 pm
Length of Visible Light: 1:56 pm
Length of Day: 1:06 pm

Diff Visable Light from Yesterday: -2 mins
Diff Length of day from Yesterday: -3 mins`

Code:

<?php
// set elsewhere
# or use on style "US/Phoenix" "US/Eastern" "US/Pacific"...
$SITE['TZ'] = "America/Phoenix";  
$SITE['LAT'] = 33.4026;
$SITE['LONG'] = -111.8831;

get_times();

echo "<h3>24 Hour Mode</h3>\n";
echo "<b>Today:</b>
\n";
echo "Sunrise: " . $VALS['sunrise'] . "
\n";
echo "Sunset: " . $VALS['sunset'] . "
\n";
echo "Civilian Twilight start: " . $VALS['civiltwstart'] . "
\n";
echo "Civilian Twilight end: " . $VALS['civiltwend'] . "
\n";
echo "Nautical Twilight start: " . $VALS['nauticaltwstart'] . "
\n";
echo "Nautical Twilight end: " . $VALS['nauticaltwend'] . "
\n";
echo "Astronomical Twilight start: " . $VALS['astrotwstart'] . "
\n";
echo "Astronomical Twilight end: " . $VALS['astrotwend'] . "
\n";
echo "Length of Visible Light: " . $VALS['lenvislight'] . "
\n";
echo "Length of Day: " . $VALS['lenofday'] . "
\n";

echo "
\n<b>Tommorrow:</b>
\n";

echo "Sunrise: " . $VALS['tsunrise'] . "
\n";
echo "Sunset: " . $VALS['tsunset'] . "
\n";
echo "Civilian Twilight start: " . $VALS['tciviltwstart'] . "
\n";
echo "Civilian Twilight end: " . $VALS['tciviltwend'] . "
\n";
echo "Nautical Twilight start: " . $VALS['tnauticaltwstart'] . "
\n";
echo "Nautical Twilight end: " . $VALS['tnauticaltwend'] . "
\n";
echo "Astronomical Twilight start: " . $VALS['tastrotwstart'] . "
\n";
echo "Astronomical Twilight end: " . $VALS['tastrotwend'] . "
\n";
echo "Length of Visible Light: " . $VALS['tlenvislight'] . "
\n";
echo "Length of Day: " . $VALS['tlenofday'] . "
\n";

echo "
\n";

echo "Diff Visable Light from Yesterday: " . 
   out_time((getTimestamp($VALS['tlenvislight']) - getTimestamp($VALS['lenvislight'])),0) .
    "
\n";
echo "Diff Length of day from Yesterday: " . 
   out_time((getTimestamp($VALS['tlenofday']) - getTimestamp($VALS['lenofday'])),0) .
    "
\n";
    
echo "<hr><h3>12 hour Mode</h3>\n";

echo "<b>Today:</b>
\n";
echo "Sunrise: " . time24to12($VALS['sunrise']) . "
\n";
echo "Sunset: " . time24to12($VALS['sunset']) . "
\n";
echo "Civilian Twilight start: " . time24to12($VALS['civiltwstart']) . "
\n";
echo "Civilian Twilight end: " . time24to12($VALS['civiltwend']) . "
\n";
echo "Nautical Twilight start: " . time24to12($VALS['nauticaltwstart']) . "
\n";
echo "Nautical Twilight end: " . time24to12($VALS['nauticaltwend']) . "
\n";
echo "Astronomical Twilight start: " . time24to12($VALS['astrotwstart']) . "
\n";
echo "Astronomical Twilight end: " . time24to12($VALS['astrotwend']) . "
\n";
echo "Length of Visible Light: " . time24to12($VALS['lenvislight']) . "
\n";
echo "Length of Day: " . time24to12($VALS['lenofday']) . "
\n";

echo "
\n<b>Tommorrow:</b>
\n";

echo "Sunrise: " . time24to12($VALS['tsunrise']) . "
\n";
echo "Sunset: " . time24to12($VALS['tsunset']) . "
\n";
echo "Civilian Twilight start: " . time24to12($VALS['tciviltwstart']) . "
\n";
echo "Civilian Twilight end: " . time24to12($VALS['tciviltwend']) . "
\n";
echo "Nautical Twilight start: " . time24to12($VALS['tnauticaltwstart']) . "
\n";
echo "Nautical Twilight end: " . time24to12($VALS['tnauticaltwend']) . "
\n";
echo "Astronomical Twilight start: " . time24to12($VALS['tastrotwstart']) . "
\n";
echo "Astronomical Twilight end: " . time24to12($VALS['tastrotwend']) . "
\n";
echo "Length of Visible Light: " . time24to12($VALS['tlenvislight']) . "
\n";
echo "Length of Day: " . time24to12($VALS['tlenofday']) . "
\n";

echo "
\n";

echo "Diff Visable Light from Yesterday: " . 
   out_time((getTimestamp($VALS['tlenvislight']) - getTimestamp($VALS['lenvislight'])),0) .
    "
\n";
echo "Diff Length of day from Yesterday: " . 
   out_time((getTimestamp($VALS['tlenofday']) - getTimestamp($VALS['lenofday'])),0) .
    "
\n";

    
exit;

function get_times() {
    global $SITE, $VALS;
    
    // Set the timezone we want to use
    set_tz( $SITE['TZ'] );
    
    // Use your location information
    $lat = $SITE['LAT'];    // South is negative
    $long = $SITE['LONG'];   // West is negative
    $timeused = $timetoday;
    $timetoday = time();
    $timetommorrow = $timetoday + 86400;
    
    $zenith=90+50/60;
    $VALS['sunrise'] = date_sunrise($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['sunset'] = date_sunset($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tsunrise'] = date_sunrise($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tsunset'] = date_sunset($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
        
    $zenith=96;
    $VALS['civiltwstart'] = date_sunrise($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['civiltwend'] = date_sunset($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tciviltwstart'] = date_sunrise($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tciviltwend'] = date_sunset($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
        
    $zenith=102;
    $VALS['nauticaltwstart'] = date_sunrise($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['nauticaltwend'] = date_sunset($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tnauticaltwstart'] = date_sunrise($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tnauticaltwend'] = date_sunset($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
        
    $zenith=108;
    $VALS['astrotwstart'] = date_sunrise($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['astrotwend'] = date_sunset($timetoday, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tastrotwstart'] = date_sunrise($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
    $VALS['tastrotwend'] = date_sunset($timetommorrow, SUNFUNCS_RET_STRING, $lat, $long, $zenith);
        
    // Other values
    $VALS['lenvislight'] = out_time((getTimestamp($VALS['civiltwend']) - getTimestamp($VALS['civiltwstart'])),1);
    $VALS['lenofday'] = out_time((getTimestamp($VALS['sunset']) - getTimestamp($VALS['sunrise'])),1);
    $VALS['tlenvislight'] = out_time((getTimestamp($VALS['tciviltwend']) - getTimestamp($VALS['tciviltwstart'])),1);
    $VALS['tlenofday'] = out_time((getTimestamp($VALS['tsunset']) - getTimestamp($VALS['tsunrise'])),1);  

}



function set_tz ($TZ){
    if (phpversion() >= "5.1.0") {
        date_default_timezone_set($TZ);
    } else {
        putenv("TZ=" . $TZ);
    }
}

function getTimestamp ($val) {
    list ($hour,$min) = preg_split("/:/",$val);
    $ttl = ($min * 60) + ($hour * 60 * 60);
    return($ttl);
}


function out_time ( $seconds , $mode = false) {
    $uday = (3600 * 24);
    $uhr = 3600;
    $umin = 60;
    
    if ($seconds < 0 ) {
        $neg = "-" ;
        $seconds = $seconds * -1 ;
        //echo "Seconds = " . $seconds . "
";
        
    } else {
        $neg = "" ;
    }
    
    // Calculate days
    $dd = intval($seconds / $uday );
    $mmremain = ($seconds - ($dd * $uday));
    
    // Calculate hours
    $hh = intval($mmremain / $uhr);    
    $ssremain = ($mmremain - ($hh * $uhr));  
    
    // Calculate minutes
    $mm = intval($ssremain / $umin);
    $ss = ($ssremain - ($mm * $umin));
    
    // If day or days
    if ($dd == 1) { $days = 'day';  }
    if ($dd > 1 ) { $days = "days"; }
    
    if (!$mode) {
        // String for if there are days
        if ( $dd > 0 ) {
            $out_string = sprintf("%d %s %02d hrs %02d mins", 
                $dd, $days, $hh, $mm);
        }
        
        // String if there are hours
        if ( $dd == 0 && $hh > 0 ) {
            $out_string = $neg . sprintf("%d hrs %02d mins", 
                $hh, $mm);
        }
        
        // String if there are minutes
        if ( $dd == 0 && $hh == 0 && $mm > 0 ) {
            $out_string = $neg . sprintf("%d mins", 
                $mm);
        }
        
        // Only output Seconds
        if ( $hh == 0 && $mm == 0 ) {
            $out_string = $neg . sprintf("%d secs", $ss) ;
        }
    } else {
        // String for if there are days
        if ( $dd > 0 ) {
            $out_string = $neg . sprintf("%d:%s %02d:%02d", 
                $dd, $days, $hh, $mm);
        }
        
        // String if there are hours
        if ( $dd == 0 && $hh > 0 ) {
            $out_string = $neg . sprintf("%d:%02d", 
                $hh, $mm);
        }
        
        // String if there are minutes
        if ( $dd == 0 && $hh == 0 && $mm > 0 ) {
            $out_string = $neg . sprintf("00:%02d", 
                $mm);
        }
        
        // Only output Seconds
        if ( $hh == 0 && $mm == 0 ) {
            $out_string = $neg . sprintf("%d secs", $ss) ;
        }       
        
    }
    
    // Return value
    return ($out_string);
}

function time24to12 ($val) {
    if (strlen($val) == 5 ) {
        $val .= ":00";
    }
    
    return( date("g:i a", strtotime($val)) );
} 

It’s an hour off for me too,

$SITE[‘TZ’] = “America/Los_Angeles”;
$SITE[‘LAT’] = 37.500;
$SITE[‘LONG’] = -122.500;

Sunrise: 05:32
Sunset: 18:51

Should be:

Saturday
23 August 2008 Pacific Daylight Time

                     SUN
    Begin civil twilight       6:06 a.m.                 
    Sunrise                    6:33 a.m.                 
    Sun transit                1:12 p.m.                 
    Sunset                     7:51 p.m.                 
    End civil twilight         8:18 p.m.             

I need to look at the clock on e-rice’s server I guess.

Nope, server is on GMT and time is correct. Must be DST which you don’t have in AZ.