cloudy

Author Topic: Sunrise - Sunset PHP Script  (Read 11859 times)

0 Members and 1 Guest are viewing this topic.

Offline daveq

  • Posts: 3,098
  • Hollywood, MD
    • Hollywood - St. Mary's County, Maryland
Re: PHP code for day/night, sunrise/sunset times
« Reply #15 on: May 22, 2008, 09:53:17 PM »
Your link looks OK to me now

Good day
Sunset will occur at 08:19 pm EDT

I had two files and selected the wrong one.   #-o

As you say it is now correct.

--Dave

Offline TNETWeather

  • Kevin Reed (KrelvinAZ)
  • Global Moderator
  • Posts: 5,859
  • Gremlins are at work...
  • Mesa, AZ
    • TNET Weather Station - Mesa AZ
Re: PHP code for day/night, sunrise/sunset times
« Reply #16 on: May 22, 2008, 09:54:48 PM »
Actually, I was looking for PHP code for one of the pairs.

The two pairs were Sunrise/Sunset  and Moonrise/Moonset

I had PHP code for one of them, but never found the other.  Can't recall which pair it was now though.  Most likely the Moon ones.


All you need is Time, Aptitude and Desire ... and you can build just about anything...

Offline MCHALLIS

  • Posts: 2,102
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: PHP code for day/night, sunrise/sunset times
« Reply #17 on: May 22, 2008, 09:59:33 PM »
Kevin,
I see you commented the putenv("TZ=$myTZ");
try again after uncommenting it, because I tried with your settings and get "Sunset will occur at 07:27 pm MST"

Offline MCHALLIS

  • Posts: 2,102
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: PHP code for day/night, sunrise/sunset times
« Reply #18 on: May 22, 2008, 10:02:54 PM »
Actually, I was looking for PHP code for one of the pairs.

The two pairs were Sunrise/Sunset  and Moonrise/Moonset

I had PHP code for one of them, but never found the other.  Can't recall which pair it was now though.  Most likely the Moon ones.



Farmers almanac does this:
http://www.almanac.com/rise/index.php

Offline TNETWeather

  • Kevin Reed (KrelvinAZ)
  • Global Moderator
  • Posts: 5,859
  • Gremlins are at work...
  • Mesa, AZ
    • TNET Weather Station - Mesa AZ
Re: PHP code for day/night, sunrise/sunset times
« Reply #19 on: May 22, 2008, 10:22:27 PM »
Kevin,
I see you commented the putenv("TZ=$myTZ");
try again after uncommenting it, because I tried with your settings and get "Sunset will occur at 07:27 pm MST"

hmmmm...

Just uncommented it out.  Still reported 8pm.

Tried again with "US/Arizona"  ... same response.

All you need is Time, Aptitude and Desire ... and you can build just about anything...

Offline daveq

  • Posts: 3,098
  • Hollywood, MD
    • Hollywood - St. Mary's County, Maryland
Re: PHP code for day/night, sunrise/sunset times
« Reply #20 on: May 22, 2008, 10:33:31 PM »
Maybe you need America/Phoenix

From Ken's web site

US   +332654-1120424   America/Phoenix   Mountain Standard Time - Arizona

--Dave
« Last Edit: May 22, 2008, 10:39:26 PM by daveq »

Offline niko

  • syzygy
  • Global Moderator
  • Posts: 21,998
  • Crystal Ball broken! Please post the URL.
  • Northern California, U.S.A.
    • http://www.realweatherstation.com
Re: PHP code for day/night, sunrise/sunset times
« Reply #21 on: May 22, 2008, 10:34:51 PM »
What $tzoffset  = -8; # pacific timezone do you have?

Offline niko

  • syzygy
  • Global Moderator
  • Posts: 21,998
  • Crystal Ball broken! Please post the URL.
  • Northern California, U.S.A.
    • http://www.realweatherstation.com
Re: PHP code for day/night, sunrise/sunset times
« Reply #22 on: May 22, 2008, 10:37:10 PM »
  $myTZ = "US/Arizona";  //NOTE: this *MUST* be set correctly

// translate UTC times to your LOCAL time for the displays.
 putenv("TZ=$myTZ");


$latitude  = 33.40; //North
$longitude = -111.88; //West
$tzoffset  = -7; # pacific timezone

=

Good day
Sunset will occur at 07:27 pm MST

Offline MCHALLIS

  • Posts: 2,102
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: PHP code for day/night, sunrise/sunset times
« Reply #23 on: May 22, 2008, 11:00:16 PM »
What $tzoffset  = -8; # pacific timezone do you have?

I modified the script earlier today (ver. 1.02), it calculates $tzoffset automatically now based on the $myTZ setting.

I also tried his settings on my server and it was within 2 minutes of WD
"WD: Sunset 07:25 pm MST"
"PHP: Sunset will occur at 07:27 pm MST"

I wonder why my server would say 07:27 pm MST and his server 08:00 pm MST  using the same settings ????

Code: [Select]
<?php
// PHP code for day/night, sunrise/sunset times
// prints is it night or day?
// prints what time is sunrise or sunset?
// **Requires PHP 5**
// by Mike Challis http://www.carmosaic.com/
// Ver. 1.02 05/22/08 fixed timezone issue

// ##### begin settings #############

// Set the timezone for your location, because some servers are in different timezone than your location
//  http://saratoga-weather.org/timezone.txt  has the list of timezone names
$myTZ 'America/Phoenix';  // NOTE: this *MUST* be set correctly

$time_format 'h:i a T'// 08:53 pm PDT

// Set the latitude and longitude for your location
$latitude  33.4026//North
$longitude = -111.8831//West

// uncomment one zenith setting: http://en.wikipedia.org/wiki/Twilight
$zenith 90+(50/60); // True sunrise/sunset
//$zenith = 96;          // Civilian Twilight - Conventionally used to signify twilight
//$zenith = 102;         // Nautical Twilight - the point at which the horizon stops being visible at sea.
//$zenith = 108;         // Astronomical Twilight - the point when Sun stops being a source of any illumination.

// ##### end of settings #############

// translate UTC times to your LOCAL time for the displays.
putenv("TZ=$myTZ");

// find time offset in hours
$tzoffset date("Z")/60 60;

$dayornight day_or_night();

if(
$dayornight == 'night') {
      
$sunrise date_sunrise(time(), SUNFUNCS_RET_STRING$latitude$longitude$zenith$tzoffset);
      
$sunrise_time date($time_formatstrtotime(date("Y-m-d") . ' '$sunrise));
      echo 
'Good night<br/>';
      echo 
'Sunrise will occur at '$sunrise_time;
}else{
      
$sunset date_sunset(time(), SUNFUNCS_RET_STRING$latitude$longitude$zenith$tzoffset);
      
$sunset_time date($time_formatstrtotime(date("Y-m-d") . ' '$sunset));
      echo 
'Good day<br/>';
      echo 
'Sunset will occur at '$sunset_time;
}

function 
day_or_night() {
   global 
$latitude$longitude$tzoffset$zenith;

   
$sunrise date_sunrise(time(), SUNFUNCS_RET_DOUBLE$latitude$longitude$zenith$tzoffset);
   
$sunset date_sunset(time(), SUNFUNCS_RET_DOUBLE$latitude$longitude$zenith$tzoffset);
   
$now date("H") + date("i") / 60 date("s") / 3600;

   if (
$sunrise $sunset)
      if ((
$now $sunrise) && ($now $sunset)) return "day";
         else return 
"night";
   else
     if ((
$now $sunrise) || ($now $sunset)) return "day";
        else return 
"night";
}


?>


Offline TNETWeather

  • Kevin Reed (KrelvinAZ)
  • Global Moderator
  • Posts: 5,859
  • Gremlins are at work...
  • Mesa, AZ
    • TNET Weather Station - Mesa AZ
Re: PHP code for day/night, sunrise/sunset times
« Reply #24 on: May 22, 2008, 11:09:38 PM »
Something is very weird...  It is acting like it is rounding the hour.

I updated the script so that it outputs the settings being used.

I am sneaking out of work now and will play with this a bit when I get home.

All you need is Time, Aptitude and Desire ... and you can build just about anything...

Offline niko

  • syzygy
  • Global Moderator
  • Posts: 21,998
  • Crystal Ball broken! Please post the URL.
  • Northern California, U.S.A.
    • http://www.realweatherstation.com
Re: PHP code for day/night, sunrise/sunset times
« Reply #25 on: May 22, 2008, 11:11:11 PM »
I think the time is off on your server:

Date: Fri, 23 May 2008 00:09:51 GMT

not that that would cause it...

Offline TNETWeather

  • Kevin Reed (KrelvinAZ)
  • Global Moderator
  • Posts: 5,859
  • Gremlins are at work...
  • Mesa, AZ
    • TNET Weather Station - Mesa AZ
Re: PHP code for day/night, sunrise/sunset times
« Reply #26 on: May 23, 2008, 12:46:42 AM »
Server is time sync'd to an atomic clock.

Request from the web server returns GMT time.

Date: Fri, 23 May 2008 01:29:33 GMT

Updated the script to output its current time in GMT and each of the 4 basic US timezones...

So you can check it against your local time.

Settings:

Server Time before anything is set by script is:
Fri, 23 May 2008 01:44:56 GMT
Thu, 22 May 2008 18:44:56 MST
Thu, 22 May 2008 21:44:56 EDT
Thu, 22 May 2008 20:44:56 CDT
Thu, 22 May 2008 19:44:56 MDT
Thu, 22 May 2008 18:44:56 PDT

Local Time: Thu, 22 May 2008 18:44:56 MST
myTZ = America/Phoenix
lat = 33.4
long = -111.88
Zenith = 90.833333333333
tzoffset = -7


Good day
Sunset will occur at 08:00 pm MST

All you need is Time, Aptitude and Desire ... and you can build just about anything...

Offline TNETWeather

  • Kevin Reed (KrelvinAZ)
  • Global Moderator
  • Posts: 5,859
  • Gremlins are at work...
  • Mesa, AZ
    • TNET Weather Station - Mesa AZ
Re: PHP code for day/night, sunrise/sunset times
« Reply #27 on: May 23, 2008, 12:57:22 AM »
Put the same code on another server and it works...

Must be something about the configuration on my server.... not sure what though...

Settings:

Server Time before anything is set by script is:
Fri, 23 May 08 01:55:46 +0000
Thu, 22 May 08 21:55:46 -0400
Thu, 22 May 08 21:55:46 -0400
Thu, 22 May 08 20:55:46 -0500
Thu, 22 May 08 19:55:46 -0600
Thu, 22 May 08 18:55:46 -0700

Local Time: Thu, 22 May 08 18:55:46 -0700
myTZ = America/Phoenix
lat = 33.4
long = -111.88
Zenith = 90.8333333333
tzoffset = -7


Good day
Sunset will occur at 07:27 pm MST

All you need is Time, Aptitude and Desire ... and you can build just about anything...

Offline niko

  • syzygy
  • Global Moderator
  • Posts: 21,998
  • Crystal Ball broken! Please post the URL.
  • Northern California, U.S.A.
    • http://www.realweatherstation.com
Re: PHP code for day/night, sunrise/sunset times
« Reply #28 on: May 23, 2008, 01:02:17 AM »
I understand that the server returns GMT, but if I query it at 11 minutes past the hour, GMT will also be 11 minutes past the hour, only the hour should be different, whereas it returned 00:09:51 GMT  :?  Seems to be OK now though  :? :?

Offline MCHALLIS

  • Posts: 2,102
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: PHP code for day/night, sunrise/sunset times
« Reply #29 on: May 23, 2008, 01:09:24 AM »
I saw Kevin testing with some debugging code, so I added some of that in.

changed code in 1st post
Ver. 1.03 05/22/08 added setting to print debugging info, also added a few default timezones, just uncomment what you need

 

cumulus