| px | top | add code | search | signup | login | help |
<?php
/******************************************************************************
The original code is written by Mmattf@mail.com - please use this code in any way you wish
and if you want to, let me know how you are using it.
Changed by Mårten Andersson, marten@odg.nu
My changes:
Made the script to a class.
Made a new function to adjust daylight savings time. (I'm not sure how good it works outside Europe)
******************************************************************************/
class sunclass{
var $date;
var $longitude;
var $latitude;
var $timezone;
var $twichoice;
var $result = array();
/******************************************************************************
setDate($year,$month,$day)
Sets the date for the sunrise/sunset calulation
Input-values:
$year : year is between 1 and 32767
$month : month is between 1 and 12
$day : Day is within the allowed number of days for the given month. Leap years are taken into consideration.
The function returns 1 if ok otherwise it returns a error message.
******************************************************************************/
function setDate($year,$month,$day){
$this->date = "";
$this->result = "";
if(checkdate($month,$day,$year)){
$this->date = "$year-$month-$day";
return 1;
}else{
return "Not a valid date!";
}
}
/******************************************************************************
setPosition($lat,$long,$tzone)
The position of the place to calculate
Input values:
$lat : float, latitude (ex. 59.00)
$long : float, longitude (ex. 18.04)
$tzone : integer, Standard time difference from Greenwich. Note that the script handles the daylight savings time!
The function returns 1 if ok otherwise it returns a error message.
******************************************************************************/
function setPosition($lat,$long,$tzone){
$this->longitude = "";
$this->latitude = "";
$this->timezone = "";
$this->result = "";
if(!is_float($lat)){
return "Latitude must be a float!";
}
if(!is_float($long)){
return "Longitude must be a float!";
}
if(!is_numeric($tzone)){
return "Timezone must be a digit!";
}
$this->longitude = $long;
$this->latitude = $lat;
$this->timezone = $tzone;
return 1;
}
/******************************************************************************
setTwilightChoice($twi)
$twi : integer, 0-3, Diffrent ways to calculate the sunrise/sunset
0 = normal twilight (sunrise/sunset) This is the most common way to calculate sunrise/sunset!
1 = nautical twilight.
2 = astronomical twilight.
3 = civil twilight
The function returns 1 if ok otherwise it returns a error message.
******************************************************************************/
function setTwilightChoice($twi){
$this->twichoice = "";
$this->result = "";
if(!is_numeric($twi)){
return "The twilightchoice must be a digit!";
}else{
if($twi<0 || $twi > 3){
return "The twilight-choice is out of range!";
}
}
$this->twichoice = $twi;
return 1;
}
/******************************************************************************
calculate()
After the parameters i set by the set functions() you can calculate the choosed
data.
Return 1 on sucess and 0 on failure
******************************************************************************/
function calculate(){
$this->result = "";
$timez = array();
$riseset = 0;
while($riseset<2){
$dateArray = explode("-",$this->date);
$timestamp = mktime(01,01,01,$dateArray[1],$dateArray[2],$dateArray[0]);
$daylightHour = date("O",$timestamp);
$daylightHour = substr($daylightHour,2,1);
$tzone = $this->timezone+($daylightHour-$this->timezone);
$yday = date(z,$timestamp);
$mon = date(n,$timestamp);
$mday = date(j,$timestamp);
$year = date(Y,$timestamp);
# DEFAULT För sol upp/ner, R = -.0145439
$R = -.0145439;
if($this->twichoice == 1){
# För nautisk skymmning
$R = -.207912;
}elseif($this->twichoice == 2){
# För astronomisk skymmning
$R = -.309017;
}elseif($this->twichoice == 3){
# "civil" skymmning
$R = -.104528;
}
$A = 1.5708;
$B = 3.14159;
$C = 4.71239;
$D = 6.28319;
$E = 0.0174533 * $this->latitude;
$F = 0.0174533 * $this->longitude;
$G = 0.261799 * $tzone;
# kalkylerar sol upp eller sol ner
if ($riseset == 1) {
$J = $C;
$type = "nedgång";
}else {
$J = $A;
$type = "uppgång";
}
$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) {return 0;}
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);
$Q = atan2($Q, 1);
$S = $R - (sin($Q) * sin($E));
$S = $S / (cos($Q) * cos($E));
if (abs($S) > 1) {$timez[] = 'none';} # Null phenomenon
$S = $S / sqrt(-$S * $S + 1);
$S = $A - atan2($S, 1);
if ($type == 'uppgång') {$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) {return 0;}
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);
$timez[] = date( "H:i", mktime($hour,$min,0,$mon,$mday,$year) );
$riseset++;
}
$this->result = $timez;
return 1;
}
/******************************************************************************
getTime()
Returns the result as an array. 0 = sunrise, 1 = sunset.
******************************************************************************/
function getTime(){
return $this->result;
}
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.