WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Functions for calculating sunrise/sunset, twilight * length of day based on latitude / longituse & date
Categories : Date Time, PHP, Complete Programs Update Picture
Brian Leyton
Date : Jan 05th 1999
Grade : 3 of 5 (graded 3 times)
Viewed : 5610
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Brian Leyton
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?
/* sunriset.inc
(c) Brian Leyton 10/28/98
May be used freely for commercial or non-commercial use, as long
as this notice is not removed.
Any comments may be sent to bleyton@bigfoot.com.

Adapted from SUNRISET.C by Paul Schlyter 05-Jul-1997
*/


/* A function to compute the number of days elapsed since 2000 Jan 0.0 */
/* (which is equal to 1999 Dec 31, 0h UT) */

function days_since_2000_Jan_0($y,$m,$d) {
$days = (int) (367*($y)-((7*(($y)+((($m)+9)/12)))/4)+((275*($m))/9)+($d)-
730530);
return $days;
}

/* Some conversion factors between radians and degrees */

define("PI",3.1415926535897932384);
define("RADEG",( 180.0 / PI ));
define("DEGRAD",( PI / 180.0 ));
define("INV360",( 1.0 / 360.0 ));

/* The trigonometric functions in degrees */

function sind($x) {
$res = (double) sin(($x)*DEGRAD);
return $res;
}

function cosd($x) {
$res = (double) cos(($x)*DEGRAD);
return $res;
}

function tand($x) {
$res = (double) tan(($x)*DEGRAD);
return $res;
}

function atand($x) {
$res = (double) RADEG*atan($x);
return $res;
}

function asind($x) {
$res = (double) RADEG*asin($x);
return $res;
}
function acosd($x) {
$res = (double) RADEG*acos($x);
return $res;
}
/* If you're running an early version of PHP, which doesn't have this
function built-in, just un-comment the following code

function atan2($x,$y) {
if($x > 0)
$res = atan($y/$x);
elseif($x < 0)
$res = atan($y/$x) + PI;
/* below $x is zero
elseif($y > 0)
$res = PI/2;
elseif($y < 0)
$res = -PI/2;
/* Below, both $x and $y are 0
else
$res = 0;
return $res;
}*/

function atan2d($y,$x) {
$res = (double) RADEG*atan2($y,$x);
return $res;
}

function sun_RA_dec( $d, &$RA, &$dec, &$r )
{
$lon = (double) 0;
$obl_ecl = (double) 0;
$x = (double) 0;
$y = (double) 0;
$z = (double) 0;

/* Compute Sun's ecliptical coordinates */
sunpos( $d, &$lon, $r );

/* Compute ecliptic rectangular coordinates (z=0) */
$x = $r * cosd($lon);
$y = $r * sind($lon);

/* Compute obliquity of ecliptic (inclination of Earth's axis) */
$obl_ecl = 23.4393 - 3.563E-7 * $d;

/* Convert to equatorial rectangular coordinates - x is unchanged */
$z = $y * sind($obl_ecl);
$y = $y * cosd($obl_ecl);

/* Convert to spherical coordinates */
$RA = atan2d( $y, $x );
$dec = atan2d( $z, sqrt($x*$x + $y*$y) );

} /* sun_RA_dec */

/******************************************************************/
/* This function reduces any angle to within the first revolution */
/* by subtracting or adding even multiples of 360.0 until the */
/* result is >= 0.0 and < 360.0 */
/******************************************************************/

function revolution( $x )
/*****************************************/
/* Reduce angle to within 0..360 degrees */
/*****************************************/
{
return( $x - 360.0 * floor( $x * INV360 ) );
} /* revolution */

function rev180( $x )
/*********************************************/
/* Reduce angle to within +180..+180 degrees */
/*********************************************/
{
return( $x - 360.0 * floor( $x * INV360 + 0.5 ) );
} /* rev180 */


/*******************************************************************/
/* This function computes GMST0, the Greenwich Mean Sidereal Time */
/* at 0h UT (i.e. the sidereal time at the Greenwhich meridian at */
/* 0h UT). GMST is then the sidereal time at Greenwich at any */
/* time of the day. I've generalized GMST0 as well, and define it */
/* as: GMST0 = GMST - UT -- this allows GMST0 to be computed at */
/* other times than 0h UT as well. While this sounds somewhat */
/* contradictory, it is very practical: instead of computing */
/* GMST like: */
/* */
/* GMST = (GMST0) + UT * (366.2422/365.2422) */
/* */
/* where (GMST0) is the GMST last time UT was 0 hours, one simply */
/* computes: */
/* */
/* GMST = GMST0 + UT */
/* */
/* where GMST0 is the GMST "at 0h UT" but at the current moment! */
/* Defined in this way, GMST0 will increase with about 4 min a */
/* day. It also happens that GMST0 (in degrees, 1 hr = 15 degr) */
/* is equal to the Sun's mean longitude plus/minus 180 degrees! */
/* (if we neglect aberration, which amounts to 20 seconds of arc */
/* or 1.33 seconds of time) */
/* */
/*******************************************************************/

function GMST0( $d )
{
$sidtim0 = (double) 0;
/* Sidtime at 0h UT = L (Sun's mean longitude) + 180.0 degr */
/* L = M + w, as defined in sunpos(). Since I'm too lazy to */
/* add these numbers, I'll let the C compiler do it for me. */
/* Any decent C compiler will add the constants at compile */
/* time, imposing no runtime or code overhead. */
$sidtim0 = revolution( ( 180.0 + 356.0470 + 282.9404 ) +
( 0.9856002585 + 4.70935E-5 ) * $d );
return $sidtim0;
} /* GMST0 */

/* The "workhorse" function for sun rise/set times */

function sunriset( $year, $month, $day, $lon, $lat, $altit, $upper_limb, &
$trise, &$tset)
/***************************************************************************/
/* Note: year,month,date = calendar date, 1801-2099 only. */
/* Eastern longitude positive, Western longitude negative */
/* Northern latitude positive, Southern latitude negative */
/* The longitude value IS critical in this function! */
/* altit = the altitude which the Sun should cross */
/* Set to -35/60 degrees for rise/set, -6 degrees */
/* for civil, -12 degrees for nautical and -18 */
/* degrees for astronomical twilight. */
/* upper_limb: non-zero -> upper limb, zero -> center */
/* Set to non-zero (e.g. 1) when computing rise/set */
/* times, and to zero when computing start/end of */
/* twilight. */
/* *rise = where to store the rise time */
/* *set = where to store the set time */
/* Both times are relative to the specified altitude, */
/* and thus this function can be used to compute */
/* various twilight times, as well as rise/set times */
/* Return value: 0 = sun rises/sets this day, times stored at */
/* *trise and *tset. */
/* +1 = sun above the specified "horizon" 24 hours. */
/* *trise set to time when the sun is at south, */
/* minus 12 hours while *tset is set to the south */
/* time plus 12 hours. "Day" length = 24 hours */
/* -1 = sun is below the specified "horizon" 24 hours */
/* "Day" length = 0 hours, *trise and *tset are */
/* both set to the time when the sun is at south. */
/* */
/**********************************************************************/
{

$d = (double) 0; /* Days since 2000 Jan 0.0 (negative before) */
$sr = (double) 0; /* Solar distance, astronomical units */
$sRA = (double) 0; /* Sun's Right Ascension */
$sdec = (double) 0; /* Sun's declination */
$sradius = (double) 0; /* Sun's apparent radius */
$t = (double) 0; /* Diurnal arc */
$tsouth = (double) 0; /* Time when Sun is at south */
$sidtime = (double) 0; /* Local sidereal time */

$rc = (int) 0; /* Return cde from function - usually 0 */

/* Compute d of 12h local mean solar time */
$d = (double) days_since_2000_Jan_0($year,$month,$day) + 0.5 - $lon/360.0;

/* Compute local sidereal time of this moment */
$sidtime = (double) revolution( GMST0($d) + 180.0 + $lon );

/* Compute Sun's RA + Decl at this moment */
sun_RA_dec( $d, &$sRA, &$sdec, &$sr );

/* Compute time when Sun is at south - in hours UT */
$tsouth = 12.0 - rev180($sidtime - $sRA)/15.0;

/* Compute the Sun's apparent radius, degrees */
$sradius = 0.2666 / $sr;

/* Do correction to upper limb, if necessary */
if ( $upper_limb )
$altit -= $sradius;

/* Compute the diurnal arc that the Sun traverses to reach */
/* the specified altitude altit: */
{
$cost = (double) ( sind($altit) - sind($lat) * sind($sdec) ) /
( cosd($lat) * cosd($sdec) );
if ( $cost >= 1.0 ) {
$rc = -1;
                 $t = 0.0; /* Sun always below altit */
         }
elseif ( $cost <= -1.0 ) {
$rc = +1;
                 $t = 12.0; /* Sun always above altit */
         }
else
$t = acosd($cost)/15.0; /* The diurnal arc, hours */
}

/* Store rise and set times - in hours UT */
$trise = $tsouth - $t;
$tset = $tsouth + $t;

return $rc;
} /* __sunriset__ */

/* This function computes the Sun's position at any instant */

function sunpos( $d, &$lon, &$r )
/******************************************************/
/* Computes the Sun's ecliptic longitude and distance */
/* at an instant given in d, number of days since */
/* 2000 Jan 0.0. The Sun's ecliptic latitude is not */
/* computed, since it's always very near 0. */
/******************************************************/
{
$M = (double) 0; /* Mean anomaly of the Sun */
$w = (double) 0; /* Mean longitude of perihelion */
/* Note: Sun's mean longitude = M + w */
$e = (double) 0; /* Eccentricity of Earth's orbit */
$ea = (double) 0; /* Eccentric anomaly */
$x = (double) 0;
$y = (double) 0; /* x, y coordinates in orbit */
$v = (double) 0; /* True anomaly */

/* Compute mean elements */
$M = revolution( 356.0470 + 0.9856002585 * $d );
$w = 282.9404 + 4.70935E-5 * $d;
$e = 0.016709 - 1.151E-9 * $d;

/* Compute true longitude and radius vector */
$ea = $M + $e * RADEG * sind($M) * ( 1.0 + $e * cosd($M) );
$x = cosd($ea) - $e;
$y = sqrt( 1.0 - $e*$e ) * sind($ea);
$r = sqrt( $x*$x + $y*$y ); /* Solar distance */
$v = atan2d( $y, $x ); /* True anomaly */
$lon = $v + $w; /* True solar longitude */
if ( $lon >= 360.0 )
$lon -= 360.0; /* Make it 0..360 degrees */
}
/* The "workhorse" function for day length */

function daylen( $year, $month, $day, $lon, $lat, $altit, $upper_limb )
/**********************************************************************/
/* Note: year,month,date = calendar date, 1801-2099 only. */
/* Eastern longitude positive, Western longitude negative */
/* Northern latitude positive, Southern latitude negative */
/* The longitude value is not critical. Set it to the correct */
/* longitude if you're picky, otherwise set to to, say, 0.0 */
/* The latitude however IS critical - be sure to get it correct */
/* altit = the altitude which the Sun should cross */
/* Set to -35/60 degrees for rise/set, -6 degrees */
/* for civil, -12 degrees for nautical and -18 */
/* degrees for astronomical twilight. */
/* upper_limb: non-zero -> upper limb, zero -> center */
/* Set to non-zero (e.g. 1) when computing day length */
/* and to zero when computing day+twilight length. */
/**********************************************************************/
{
$d = (double) 0; /* Days since 2000 Jan 0.0 (negative before) */
$obl_ecl = (double) 0; /* Obliquity (inclination) of Earth's axis */
$sr = (double) 0; /* Solar distance, astronomical units */
$slon = (double) 0; /* True solar longitude */
$sin_sdecl = (double) 0; /* Sine of Sun's declination */
$cos_sdecl = (double) 0; /* Cosine of Sun's declination */
$sradius = (double) 0; /* Sun's apparent radius */
$t = (double) 0; /* Diurnal arc */

/* Compute d of 12h local mean solar time */
$d = days_since_2000_Jan_0($year,$month,$day) + 0.5 - $lon/360.0;

/* Compute obliquity of ecliptic (inclination of Earth's axis) */
$obl_ecl = 23.4393 - 3.563E-7 * $d;

/* Compute Sun's position */
sunpos( $d, &$slon, &$sr );

/* Compute sine and cosine of Sun's declination */
$sin_sdecl = sind($obl_ecl) * sind($slon);
$cos_sdecl = sqrt( 1.0 - $sin_sdecl * $sin_sdecl );

/* Compute the Sun's apparent radius, degrees */
$sradius = 0.2666 / $sr;

/* Do correction to upper limb, if necessary */
if ( $upper_limb )
$altit -= $sradius;

/* Compute the diurnal arc that the Sun traverses to reach */
/* the specified altitude altit: */
{
$cost = (double)( sind($altit) - sind($lat) * $sin_sdecl ) /
( cosd($lat) * $cos_sdecl );
if ( $cost >= 1.0 )
$t = 0.0; /* Sun always below altit */
else if ( $cost <= -1.0 )
$t = 24.0; /* Sun always above altit */
else $t = (2.0/15.0) * acosd($cost); /* The diurnal arc, hours */
}
return $t;
} /* __daylen__ */


/* Following are some functions around the "workhorse" function "daylen" */
/* They mainly fill in the desired values for the reference altitude */
/* below the horizon, and also selects whether this altitude should */
/* refer to the Sun's center or its upper limb. */

/* This function computes the length of the day, from sunrise to sunset. */
/* Sunrise/set is considered to occur when the Sun's upper limb is */
/* 35 arc minutes below the horizon (this accounts for the refraction */
/* of the Earth's atmosphere). */
function day_length($year,$month,$day,$lon,$lat) {
$res=daylen( $year, $month, $day, $lon, $lat, -35.0/60.0, 1 );
return $res;
}

/* This function computes the length of the day, including civil twilight. */
/* Civil twilight starts/ends when the Sun's center is 6 degrees below */
/* the horizon. */
function day_civil_twilight_length($year,$month,$day,$lon,$lat) {
$res = daylen( $year, $month, $day, $lon, $lat, -6.0, 0 );
return $res;
}

/* This function computes the length of the day, incl. nautical twilight. */
/* Nautical twilight starts/ends when the Sun's center is 12 degrees */
/* below the horizon. */
function day_nautical_twilight_length($year,$month,$day,$lon,$lat) {
$res = daylen( $year, $month, $day, $lon, $lat, -12.0, 0 );
return $res;
}

/* This function computes the length of the day, incl. astronomical */
/* twilight. Astronomical twilight starts/ends when the Sun's center is */
/* 18 degrees below the horizon. */
function day_astronomical_twilight_length($year,$month,$day,$lon,$lat) {
$res = daylen( $year, $month, $day, $lon, $lat, -18.0, 0 );
return $res;
}


/* This function computes times for sunrise/sunset. */
/* Sunrise/set is considered to occur when the Sun's upper limb is */
/* 35 arc minutes below the horizon (this accounts for the refraction */
/* of the Earth's atmosphere). */
function sun_rise_set($year,$month,$day,$lon,$lat,$rise,$set) {
$res = sunriset($year,$month,$day,$lon,$lat, -35.0/60.0, 1,$rise,$set );
return $res;
}

/* This function computes the start and end times of civil twilight. */
/* Civil twilight starts/ends when the Sun's center is 6 degrees below */
/* the horizon. */
function civil_twilight($year,$month,$day,$lon,$lat,$start,$end) {
$res = sunriset( $year, $month, $day, $lon, $lat, -6.0, 0, $start, $end );
return $res;
}

/* This function computes the start and end times of nautical twilight. */
/* Nautical twilight starts/ends when the Sun's center is 12 degrees */
/* below the horizon. */
function nautical_twilight($year,$month,$day,$lon,$lat,$start,$end) {
$res = sunriset( $year, $month, $day, $lon, $lat, -12.0, 0, $start, $end );
}

/* This macro computes the start and end times of astronomical twilight. */
/* Astronomical twilight starts/ends when the Sun's center is 18 degrees */
/* below the horizon. */
function astronomical_twilight($year,$month,$day,$lon,$lat,$start,$end) {
$res = sunriset( $year, $month, $day, $lon, $lat, -18.0, 0, $start, $end );
return $res;
}

?>



A simple and fast calendar combining PHP and tables. Use this as a base for applications in which a calendar is needed.
Categories : Date Time, PHP, Complete Programs, Calendar
This code consists of two parts, the first part is a .html file that uses a form to pass 3 date variables into day_of_week.php3 You input the mm/dd/yyyy of the day then it prints what day of the week that day falls on.
Categories : Date Time, PHP, Complete Programs
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
phpAds, a complete banner and ad management system with detailled tracking and stats.
Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
Searches through a local INN server's discussions
Categories : Search, Complete Programs, PHP
A Timing Class
Categories : PHP, PHP Classes, Date Time
Parses HTTP_USER_AGENT so that you can customize your site to different browsers
Categories : HTML, PHP, Complete Programs
Finding the day of the week for a specific date.
Categories : PHP, Databases, MySQL, Date Time
A wrapper function to format dates coming from a databases with the same syntax as PHP's date() function.
Categories : Date Time, Databases, PHP
File Explorer, browse, upload, download and edit your web site files with only a browser and a HTTP connection.
Categories : Complete Programs, Content Management, Filesystem, PHP
PHPCatalog
Categories : Ecommerce, PHP, Complete Programs
pcCalendar class - Allows for the creation of calendars in HTML pages. All output functions can be easily overridden, refer to article 1471 for an example.
Categories : PHP, Date Time, Calendar, PHP Classes
A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Categories : PHP, PHP Classes, Calendar, Date Time
GuestBook Light - a plug and play application for any website.
Categories : PHP, Complete Programs, Filesystem, Sessions