Results 1 to 2 of 2

Thread: PHP Sunrise Sunset Script Alteration

  1. #1
    maciek Guest

    PHP Sunrise Sunset Script Alteration

    Here is the script to calculate sunset sunrise anywhere around the world

    PHP Code:
    <?php class sun
    {
        var 
    $latitude;     #szerokosc geograficzna
        
    var $longitude;    #dlugosc geograficzna
        
    var $timezone;     #strefa czasowa
            
    function sun ($latitude$longitude$timezone)
                {
                    
    $this->latitude $latitude;
                    
    $this->longitude $longitude;
                    
    $this->timezone $timezone;
                    
    $this->yday date("z");
                    
    $this->mon date("n");
                    
    $this->mday date("j");
                    
    $this->year date("Y");
                    
    #---------------------
                    
    $this->DST=$this->is_daylight_time(date("U"));
                        if (
    $this->DST)
                           {
                              
    $this->timezone = ($this->timezone 1);
                           }
                        if (
    $this->timezone == "13")
                           {
                              
    $this->timezone "-11";
                           }
                    
    #---------------------
                    
    $this->1.5708;
                    
    $this->3.14159;
                    
    $this->4.71239;
                    
    $this->6.28319;
                    
    $this->0.0174533 $this->latitude;
                    
    $this->0.0174533 $this->longitude;
                    
    $this->0.261799  $this->timezone;
                    
    #---------------------
                      # For astronomical twilight, use
                      #$this->R = -.309017;
                      # For     nautical twilight, use
                      #$this->R = -.207912;
                      # For        civil twilight, use
                      #$this->R = -.104528;
                      # For     sunrise or sunset, use
                      
    $this->= -.0145439;
                    
    #---------------------
                    
                
    }
            function 
    is_daylight_time($time)
                {
                   list(
    $dom$dow$month$hour$min) = explode(":"date("d:w:m:H:i"$time));
                   if   (
    $month && $month 10)
                      {
                             
    $this->retval 1;        # May thru September
                      
    }
                   elseif (
    $month == && $dom 7)
                      {
                             
    $this->retval 1;        # After first week in April
                      
    }
                   elseif (
    $month == && $dom <= && $dow == && $hour >= 2)
                      {
                             
    $this->retval 1;        # After 2am on first Sunday ($dow=0) in April
                      
    }
                   elseif (
    $month == && $dom <= && $dow != && ($dom-$dow 0))
                      {
                             
    $this->retval 1;        # After Sunday of first week in April
                      
    }
                   elseif (
    $month == 10 && $dom 25)
                      {
                             
    $this->retval 1;        # Before last week of October
                      
    }
                   elseif (
    $month == 10 && $dom >= 25 && $dow == && $hour 2)
                      {
                             
    $this->retval 1;        # Before 2am on last Sunday in October
                      
    }
                   elseif (
    $month == 10 && $dom >= 25 && $dow != && ($dom-24-$dow 1) )
                      {
                             
    $this->retval 1;        # Before Sunday of last week in October
                      
    }
                   else
                      {
                             
    $this->retval 0;
                      }

                      

                    return 
    $this->retval;
            }
        function 
    sunrise()
            {
                
    $J =  $this->A;
                
    $K $this->yday + (($J $this->F) / $this->D);
                
    $L = ($K .017202) - .0574039;              # Solar Mean Anomoly
                
    $M $L .0334405 sin($L);                # Solar True Longitude
                
    $M += 4.93289 + (3.49066E-04) * sin($L);
                    if (
    $this->== 0)
                      {
                         echo 
    "Trying to normalize with zero offset..."; exit;
                      }
                    while (
    $M 0)
                      {
                         
    $M = ($M $this->D);
                      }
                    while (
    $M >= $this->D)
                      {
                         
    $M = ($M $this->D);
                      }
                    if ((
    $M $this->A) - intval($M $this->A) == 0)
                      {
                         
    $M += 4.84814E-06;
                      }
                
    $P sin($M) / cos($M);                   # Solar Right Ascension
                
    $P atan2(.91746 $P1);
                
    # Quadrant Adjustment
                    
    if ($M $this->C)
                      {
                         
    $P += $this->D;
                      }
                    else
                      {
                         if (
    $M $this->A)
                            {
                               
    $P += $this->B;
                            }
                      }

                
    $Q .39782 sin($M);            # Solar Declination
                
    $Q $Q sqrt(-$Q $Q 1);     # This is how the original author wrote it!
                
    $Q atan2($Q1);
                
    $S $this->- (sin($Q) * sin($this->E));
                
    $S $S / (cos($Q) * cos($this->E));
                    if (
    abs($S) > 1)
                        {
                            echo 
    'none';
                        }     
    # Null phenomenon
                
    $S $S sqrt(-$S $S 1);
                
    $S $this->atan2($S1);
                
    $S $this->$S ;
                
    $T $S $P 0.0172028 $K 1.73364# Local apparent time
                
    $U $T $this->F;                            # Universal timer
                
    $V $U $this->G;                            # Wall clock time
                # Quadrant Determination
                    
    if ($this->== 0)
                        {
                            echo 
    "Trying to normalize with zero offset..."; exit;
                        }
                    while (
    $V 0)
                        {
                            
    $V = ($V $this->D);
                        }
                    while (
    $V >= $this->D)
                        {
                            
    $V = ($V $this->D);
                        }
                
    $V $V 3.81972;
                
    $hour intval($V);
                
    $min  intval((($V $hour) * 60) + 0.5);
                echo 
    date"G:i "mktime($hour,$min,0,$this->mon,$this->mday,$this->year) );

            }
        function 
    sunset()
            {
                
    $J =  $this->C;
                
    $K $this->yday + (($J $this->F) / $this->D);
                
    $L = ($K .017202) - .0574039;              # Solar Mean Anomoly
                
    $M $L .0334405 sin($L);                # Solar True Longitude
                
    $M += 4.93289 + (3.49066E-04) * sin($L);
                    if (
    $this->== 0)
                      {
                         echo 
    "Trying to normalize with zero offset..."; exit;
                      }
                    while (
    $M 0)
                      {
                         
    $M = ($M $this->D);
                      }
                    while (
    $M >= $this->D)
                      {
                         
    $M = ($M $this->D);
                      }
                    if ((
    $M $this->A) - intval($M $this->A) == 0)
                      {
                         
    $M += 4.84814E-06;
                      }
                
    $P sin($M) / cos($M);                   # Solar Right Ascension
                
    $P atan2(.91746 $P1);
                
    # Quadrant Adjustment
                    
    if ($M $this->C)
                      {
                         
    $P += $this->D;
                      }
                    else
                      {
                         if (
    $M $this->A)
                            {
                               
    $P += $this->B;
                            }
                      }

                
    $Q .39782 sin($M);            # Solar Declination
                
    $Q $Q sqrt(-$Q $Q 1);     # This is how the original author wrote it!
                
    $Q atan2($Q1);
                
    $S $this->- (sin($Q) * sin($this->E));
                
    $S $S / (cos($Q) * cos($this->E));
                    if (
    abs($S) > 1)
                        {
                            echo 
    'none';
                        }     
    # Null phenomenon
                
    $S $S sqrt(-$S $S 1);
                
    $S $this->atan2($S1);
                
    #$S = $this->D - $S ;
                
    $T $S $P 0.0172028 $K 1.73364# Local apparent time
                
    $U $T $this->F;                            # Universal timer
                
    $V $U $this->G;                            # Wall clock time
                # Quadrant Determination
                    
    if ($this->== 0)
                        {
                            echo 
    "Trying to normalize with zero offset..."; exit;
                        }
                    while (
    $V 0)
                        {
                            
    $V = ($V $this->D);
                        }
                    while (
    $V >= $this->D)
                        {
                            
    $V = ($V $this->D);
                        }
                
    $V $V 3.81972;
                
    $hour intval($V);
                
    $min  intval((($V $hour) * 60) + 0.5);
                echo 
    date"G:i "mktime($hour,$min,0,$this->mon,$this->mday,$this->year) );
            }

    }
    ?>
    Here is the script to display the information

    PHP Code:
    <?php
    require_once('sun_class.php');

    $sun = new sun('50''21''1');
    $sun->sunrise();
    $sun->sunset();
    ?>
    I have an issue with making the TIME of sunset and sunrise a useful variable since

    $sun->sunrise();
    $sun->sunset();

    are not useful variables. Lets say $sun->sunrise(); puts out 09:50 AM. I would like to use this information for other functions.

    $TIME = $sun->sunrise(); = 09:50 AM

    How can I do this?

  2. #2
    Join Date
    Jun 2010
    Posts
    18
    Quote Originally Posted by maciek View Post
    $TIME = $sun->sunrise(); = 09:50 AM

    How can I do this?
    Try to replace last rows in both function :
    PHP Code:
    echo date"G:i "mktime($hour,$min,0,$this->mon,$this->mday,$this->year) ); 
    to
    PHP Code:
    return date"G:i "mktime($hour,$min,0,$this->mon,$this->mday,$this->year) ); 
    and
    PHP Code:
    $TIME $sun->sunrise(); 
    must be useful variables!

    If you want to print data again "$sun->sunrise(); = 09:50 AM" have to do this
    PHP Code:
     echo $sun->sunrise(); 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •