Problems getting Sunrise/Sunset times to show as Day or Nigh

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
linhardt
Forum Newbie
Posts: 2
Joined: Tue Oct 21, 2003 10:15 am
Location: Missouri

Problems getting Sunrise/Sunset times to show as Day or Nigh

Post by linhardt »

The following code works very well in producing sunrise/sunset times base on location. But I am in need of script that would let me know if it is Daytime or Nighttime. I have been trying to get the following code to post as "D" - Day or "N" - Night based on the sunrise/sunset times the PHP code produces.

I am a newbie at PHP, and this one seems to be just a loop to produce both times and confuses me. Not sure where or how to put in code or variables produce whether it is day or night, instead of the just the times.

Any advice would be appreciated.
Thanks!

Code: Select all

<? 

# This code will give you the sunrise and sunset times for any 
# latutude and longitude in the world. You just need to supply 
# the latitude, longitude and difference from GMT. 
# 
# for results in areas not using Daylight Savings Time, delete all 
# references to the is_daylight_time() function 
# 
# send the variables $latitude, $longitude and $timezone 
# This script includes is_daylight_time() function from Steve Edberg 
# and perl code translated from the perl module Astro-SunTime-0.01 
# PHP code [email]mattf@mail.com[/email] - please use this code in any way you wish 
# and if you want to, let me know how you are using it. 

if (!$latitude) {$latitude = 38.35;}     #if not set use test variable 
if (!$longitude) {$longitude = -92.10;} #if not set use test variable 
if (!$timezone) {$timezone = -6;}           #if not set use test variable 

   $yday = date(z); 
   $mon = date(n); 
   $mday = date(j); 
   $year = date(Y); 

function is_daylight_time($time) 
{ 

list($dom, $dow, $month, $hour, $min) = explode(":", date("d:w:m:H:i", $time)); 

if   ($month > 4 && $month < 10) { 
  $retval = 1;        # May thru September 
} elseif ($month == 4 && $dom > 7) { 
  $retval = 1;        # After first week in April 
} elseif ($month == 4 && $dom <= 7 && $dow == 0 && $hour >= 2) { 
  $retval = 1;        # After 2am on first Sunday ($dow=0) in April 
} elseif ($month == 4 && $dom <= 7 && $dow != 0 && ($dom-$dow > 0)) { 
  $retval = 1;        # After Sunday of first week in April 
} elseif ($month == 10 && $dom < 25) { 
  $retval = 1;        # Before last week of October 
} elseif ($month == 10 && $dom >= 25 && $dow == 0 && $hour < 2) { 
  $retval = 1;        # Before 2am on last Sunday in October 
} elseif ($month == 10 && $dom >= 25 && $dow != 0 && ($dom-24-$dow < 1) ) { 
  $retval = 1;        # Before Sunday of last week in October 
} else { 
  $retval = 0; 
} 

return($retval); 
} 

$DST=is_daylight_time(date(U)); 
if ($DST) {$timezone = ($timezone + 1);} 
if ($timezone == "13") {$timezone = "-11";} 

   $A = 1.5708; 
   $B = 3.14159; 
   $C = 4.71239; 
   $D = 6.28319;      
   $E = 0.0174533 * $latitude; 
   $F = 0.0174533 * $longitude; 
   $G = 0.261799  * $timezone; 

   # For astronomical twilight, use R = -.309017 
   # For     nautical twilight, use R = -.207912 
   # For        civil twilight, use R = -.104528 
   # For     sunrise or sunset, use R = -.0145439 
   $R = -.0145439; 





for ($i = 0; $i < 2; $i++) 
{ 

if (!$i) {$J =  $A;  $type = "rise";}    # calculate sunrise 
  else  {$J =  $C;  $type = "set"; }  # calculate sunset 

   $K = $yday + (($J - $F) / $D); 
   $L = ($K * .017202) - .0574039;              # Solar Mean Anomoly 
   $M = $L + .0334405 * sin($L);                # Solar True Longitude 
   $M += 4.93289 + (3.49066E-04) * sin(2 * $L); 
  # Quadrant Determination 
     if ($D == 0) {echo "Trying to normalize with zero offset..."; exit;} 
     while ($M < 0)   {$M = ($M + $D);} 
     while ($M >= $D) {$M = ($M - $D);} 

   if (($M / $A) - intval($M / $A) == 0) {$M += 4.84814E-06;} 
   $P = sin($M) / cos($M);                   # Solar Right Ascension 
   $P = atan2(.91746 * $P, 1); 

   # Quadrant Adjustment 
   if ($M > $C) {$P += $D;} 
   else {if ($M > $A) {$P += $B;} } 

   $Q = .39782 * sin($M);            # Solar Declination 
   $Q = $Q / sqrt(-$Q * $Q + 1);     # This is how the original author wrote it! 
   $Q = atan2($Q, 1); 

   $S = $R - (sin($Q) * sin($E)); 
   $S = $S / (cos($Q) * cos($E)); 

   if (abs($S) > 1) {echo 'none';}     # Null phenomenon 

   $S = $S / sqrt(-$S * $S + 1); 
   $S = $A - atan2($S, 1); 

   if ($type == 'rise') {$S = $D - $S ;} 

   $T = $S + $P - 0.0172028 * $K - 1.73364; # Local apparent time 
   $U = $T - $F;                            # Universal timer 
   $V = $U + $G;                            # Wall clock time 
  # Quadrant Determination 
     if ($D == 0) {echo "Trying to normalize with zero offset..."; exit;} 
     while ($V < 0)   {$V = ($V + $D);} 
     while ($V >= $D) {$V = ($V - $D);} 
   $V = $V * 3.81972; 

   $hour = intval($V); 
   $min  = intval((($V - $hour) * 60) + 0.5); 

   echo "sun$type is at: "; 

   echo date( "g:i A", mktime($hour,$min,0,$mon,$mday,$year) ); 
   echo "<br>"; 
} 

?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Well, it depends after what hour you class as night-time and when night-time becomes day-time!?

i.e
Night-time = 17:00 - 03:59
Day-time = 04:00 - 16:49


Mark
User avatar
Saethyr
Forum Contributor
Posts: 182
Joined: Thu Sep 25, 2003 9:21 am
Location: Wichita, Kansas USA
Contact:

Post by Saethyr »

Replace:

Code: Select all

if ($type == 'rise') {$S = $D - $S ;} 

   $T = $S + $P - 0.0172028 * $K - 1.73364; # Local apparent time 
   $U = $T - $F;                            # Universal timer 
   $V = $U + $G;                            # Wall clock time 
  # Quadrant Determination 
     if ($D == 0) {echo "Trying to normalize with zero offset..."; exit;} 
     while ($V < 0)   {$V = ($V + $D);} 
     while ($V >= $D) {$V = ($V - $D);} 
   $V = $V * 3.81972; 

   $hour = intval($V); 
   $min  = intval((($V - $hour) * 60) + 0.5); 

   echo "sun$type is at: "; 

   echo date( "g:i A", mktime($hour,$min,0,$mon,$mday,$year) ); 
   echo "<br>"; 
} 

?>
With:

Code: Select all

if ($type == 'rise') {$S = $D - $S ; $identifier = "D";} else {$identifier = "N";} 

   $T = $S + $P - 0.0172028 * $K - 1.73364; # Local apparent time 
   $U = $T - $F;                            # Universal timer 
   $V = $U + $G;                            # Wall clock time 
  # Quadrant Determination 
     if ($D == 0) {echo "Trying to normalize with zero offset..."; exit;} 
     while ($V < 0)   {$V = ($V + $D);} 
     while ($V >= $D) {$V = ($V - $D);} 
   $V = $V * 3.81972; 

   $hour = intval($V); 
   $min  = intval((($V - $hour) * 60) + 0.5); 

   echo "sun$type is at: "; 

   echo date( "g:i A", mktime($hour,$min,0,$mon,$mday,$year) ); 
   echo "&nbsp;$identifier<br>"; 
} 

?>

**As an added note, if you wanted to do this based on time let me know and I will work something out. I really do not understand why you want it to show day or night as it already shows AM and PM and sunrise will most always be day and sunset will be night unless you live in alaska or somewhere else in the frozen north.***

It's a hack but it works.

Saethyr
linhardt
Forum Newbie
Posts: 2
Joined: Tue Oct 21, 2003 10:15 am
Location: Missouri

Post by linhardt »

Thank you for the replies.

Yes, I am needing it to be based off of the time.

For example, if the user pulls up the application it will set the application in Nighttime Mode if it is after the sunset time and before the sunrise time, or it will set it to Daytime Mode if after sunrise time and before sunset time. I already have the code set up to make the application in Nighttime or Daytime Mode and is based off of "N" or "D".

Here is an example of the Javascript I have for it based off of specific times.

Code: Select all

var _dn_DayStart  = 0600;
var _dn_DayEnd    = 1800;


// set color mode
   if (M != "D" && M != "N") &#123; //(M == "A") &#123;
      // auto mode based on computer clock
      TDate = new Date();
      var _hour = "";
      if (TDate.getHours() < 10) &#123; _hour += "0"; &#125;
      _hour += ("" + TDate.getHours());
      if (TDate.getMinutes() < 10) &#123; _hour += "0"; &#125;
      _hour += ("" + TDate.getMinutes());
      if (_hour >= _dn_DayStart && _hour <= _dn_DayEnd) &#123; M = "D"; &#125; else &#123;M = "N"; &#125;
   &#125;
   if (M == "D") &#123; N = 0; &#125; else &#123; N = 1; &#125;
When the computer time is after 6:00 AM or before 6:00 PM (18:00) it reflects "D" otherwise it is "N". I just need the PHP code to reflect a variable such as $output = "N or D" based off the computer time.

So that I can work it into the javascript that is already setup for an existing application, and can implement (as an addition) it into the following javascript gobal variable in the application.

Code: Select all

$output=new Array() 
      var $output="<?php echo $output ?>"
No need to worry about different computer times from other places, cause this will be on local (in-house) machines only. Hence why I have the set lat. and long. and timezone offset for a particular location.

Does this better explain what I am needing? I just do not know how to get this type of results from this particular PHP code.
Post Reply