MCHALLIS
Rain Maker
 
OfflineLocation: Long Beach, WA USA Station Type: Davis VP2+
Posts: 1,997 Local Time: Thursday 11:26
|
 |
« on: May 22, 2008, 04:16:33 PM » |
|
Have you ever wanted to print is it night or day, or what time is sunrise or sunset? Perhaps you want to display something on your site only during daytime or night time. You can do this with weather display tags (and I do), but here is how using pure PHP. Requires PHP 5 (version 5.1.3 or higher recommended, due to date_sunset bug fixes in 5.1.3) Sunrise - Sunset PHP Script by Mike Challis http://www.642weather.com/weather/scripts-sunrise-sunset.php
|
|
|
|
« Last Edit: June 24, 2009, 01:59:59 AM by MCHALLIS »
|
Logged
|
|
|
|
niko
syzygy
Junior Climate Controller

OfflineLocation: Northern California, U.S.A. Station Type: 2 x Davis VP2 Wireless / Win 2000
Posts: 18,782 Local Time: Thursday 11:26
Crystal Ball broken! Please post the URL.
|
 |
« Reply #1 on: May 22, 2008, 04:35:09 PM » |
|
Hmmm, not exactly, for me it returns:
Good day Sunset will occur at 07:53 pm UTC
using the lat/long (I assume yours) that's in the code.
|
|
|
|
|
Logged
|
|
|
|
MCHALLIS
Rain Maker
 
OfflineLocation: Long Beach, WA USA Station Type: Davis VP2+
Posts: 1,997 Local Time: Thursday 11:26
|
 |
« Reply #2 on: May 22, 2008, 04:54:39 PM » |
|
It works on my server because my server is setup to use America/Los_Angeles timezone. niko, can you do me a favor and put this in the top of the script, then try it and let me know: // Set the timezone for your location, because some servers are in different timezone than your location $myTZ = "America/Los_Angeles"; //NOTE: this *MUST* be set correctly
// translate UTC times to your LOCAL time for the displays. putenv("TZ=$myTZ");
If it works I will edit my post and add it in. Thanks
|
|
|
|
|
Logged
|
|
|
|
niko
syzygy
Junior Climate Controller

OfflineLocation: Northern California, U.S.A. Station Type: 2 x Davis VP2 Wireless / Win 2000
Posts: 18,782 Local Time: Thursday 11:26
Crystal Ball broken! Please post the URL.
|
 |
« Reply #3 on: May 22, 2008, 05:09:21 PM » |
|
http://www.realweatherstation.com/FILES/daynight.phpGood now. I guess the other way would be to get the server tz setting and adjust accordingly?
|
|
|
|
|
Logged
|
|
|
|
MCHALLIS
Rain Maker
 
OfflineLocation: Long Beach, WA USA Station Type: Davis VP2+
Posts: 1,997 Local Time: Thursday 11:26
|
 |
« Reply #4 on: May 22, 2008, 05:36:18 PM » |
|
Ver. 1.01 fixed timezone issue
see code in 1st post
Anyone want to test it and let me know if you have any timezone issues?
|
|
|
|
« Last Edit: May 22, 2008, 05:38:13 PM by MCHALLIS »
|
Logged
|
|
|
|
daveq
Junior Weather Controller
  
OfflineLocation: Hollywood, MD Station Type: Davis VP2 w/FARS
Posts: 3,097 Local Time: Thursday 13:26
|
 |
« Reply #5 on: May 22, 2008, 07:54:56 PM » |
|
I tried but keep getting: Good day Sunset will occur at 12:18 am UTC$myTZ = 'America/New_York'; // 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 = 38.36; //North $longitude = -76.62; //West Most likely my fault.  --Dave
|
|
|
|
|
Logged
|
|
|
|
niko
syzygy
Junior Climate Controller

OfflineLocation: Northern California, U.S.A. Station Type: 2 x Davis VP2 Wireless / Win 2000
Posts: 18,782 Local Time: Thursday 11:26
Crystal Ball broken! Please post the URL.
|
 |
« Reply #6 on: May 22, 2008, 08:06:59 PM » |
|
You're missing the putenv("TZ=$myTZ"); line // Set the timezone for your location, because some servers are in different timezone than your location $myTZ = "America/Los_Angeles"; //NOTE: this *MUST* be set correctly
// translate UTC times to your LOCAL time for the displays. putenv("TZ=$myTZ");
Looks like Mike missed it when updating the posted code.
|
|
|
|
|
Logged
|
|
|
|
daveq
Junior Weather Controller
  
OfflineLocation: Hollywood, MD Station Type: Davis VP2 w/FARS
Posts: 3,097 Local Time: Thursday 13:26
|
 |
« Reply #7 on: May 22, 2008, 08:20:36 PM » |
|
Here is what I did but not get an error message. Did I put it in the wrong place? // 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/New_York'; // NOTE: this *MUST* be set correctly
// translate UTC times to your LOCAL time for the displays. putenv("TZ=$myTZ"); $time_format = 'h:i a T'; // 08:53 pm PDT
// Set the latitude and longitude for your location $latitude = 38.36; //North $longitude = -76.62; //West
|
|
|
|
|
Logged
|
|
|
|
niko
syzygy
Junior Climate Controller

OfflineLocation: Northern California, U.S.A. Station Type: 2 x Davis VP2 Wireless / Win 2000
Posts: 18,782 Local Time: Thursday 11:26
Crystal Ball broken! Please post the URL.
|
 |
« Reply #8 on: May 22, 2008, 08:21:59 PM » |
|
Your link looks OK to me now
Good day Sunset will occur at 08:19 pm EDT
|
|
|
|
|
Logged
|
|
|
|
MCHALLIS
Rain Maker
 
OfflineLocation: Long Beach, WA USA Station Type: Davis VP2+
Posts: 1,997 Local Time: Thursday 11:26
|
 |
« Reply #9 on: May 22, 2008, 08:23:54 PM » |
|
I fixed the code in my post, sorry about that, I was in a hurry and going out the door when I last uploaded.
|
|
|
|
|
Logged
|
|
|
|
TNETWeather
Kevin Reed (KrelvinAZ)
Weather Controller
   
OfflineLocation: Mesa, AZ Station Type: Davis Vantage Pro2 Plus W/FARS Wireless
Posts: 5,577 Local Time: Thursday 11:26
Gremlins are at work...
|
 |
« Reply #10 on: May 22, 2008, 09:19:06 PM » |
|
Using "America/Phoenix" or "US/Arizona" I get:
Good day Sunset will occur at 01:00 am MST
The MST is right (we don't do daylight savings).
Sunset however should be around 7:25pm not 1am.
Note that the server is located in Arizona and is already set to US/Arizona, commenting out the putenv made no difference.
|
|
|
|
« Last Edit: May 22, 2008, 09:20:57 PM by TNETWeather »
|
Logged
|
All you need is Time, Aptitude and Desire ... and you can build just about anything...
|
|
|
MCHALLIS
Rain Maker
 
OfflineLocation: Long Beach, WA USA Station Type: Davis VP2+
Posts: 1,997 Local Time: Thursday 11:26
|
 |
« Reply #11 on: May 22, 2008, 09:21:05 PM » |
|
Using "America/Phoenix" or "US/Arizona" I get:
Good day Sunset will occur at 01:00 am MST
The MST is right (we don't do daylight savings).
Note that the server is located in Arizona and is already set to US/Arizona, commenting out the putenv made no difference.
what are your settings? lat / long
|
|
|
|
« Last Edit: May 22, 2008, 09:23:14 PM by MCHALLIS »
|
Logged
|
|
|
|
TNETWeather
Kevin Reed (KrelvinAZ)
Weather Controller
   
OfflineLocation: Mesa, AZ Station Type: Davis Vantage Pro2 Plus W/FARS Wireless
Posts: 5,577 Local Time: Thursday 11:26
Gremlins are at work...
|
 |
« Reply #12 on: May 22, 2008, 09:32:08 PM » |
|
oops... didn't change those... Changed to: $latitude = 33.4026; //North $longitude = -111.8831; //West Now it says... Good day Sunset will occur at 08:00 pm MST WD is reporting 7:25pm but I don't know what it is using for sunset calculations. BTW I've been working on a data entry webapp that uses zipcodes to get timezones and lat/long settings... Not as accurate but close enough. temp link to script: http://www.tnetweather.com/testit.txt
|
|
|
|
|
Logged
|
All you need is Time, Aptitude and Desire ... and you can build just about anything...
|
|
|
MCHALLIS
Rain Maker
 
OfflineLocation: Long Beach, WA USA Station Type: Davis VP2+
Posts: 1,997 Local Time: Thursday 11:26
|
 |
« Reply #13 on: May 22, 2008, 09:40:59 PM » |
|
BTW I've been working on a data entry webapp that uses zipcodes to get timezones and lat/long settings... Not as accurate but close enough.
Combine that with this and you have a sunrise, sunset lookup by zip code application 
|
|
|
|
|
Logged
|
|
|
|
niko
syzygy
Junior Climate Controller

OfflineLocation: Northern California, U.S.A. Station Type: 2 x Davis VP2 Wireless / Win 2000
Posts: 18,782 Local Time: Thursday 11:26
Crystal Ball broken! Please post the URL.
|
 |
« Reply #14 on: May 22, 2008, 09:44:23 PM » |
|
oops... didn't change those... Changed to: $latitude = 33.4026; //North $longitude = -111.8831; //West Now it says... Good day Sunset will occur at 08:00 pm MST WD is reporting 7:25pm but I don't know what it is using for sunset calculations. BTW I've been working on a data entry webapp that uses zipcodes to get timezones and lat/long settings... Not as accurate but close enough. temp link to script: http://www.tnetweather.com/testit.txtWD's sunset is correct 
|
|
|
|
|
Logged
|
|
|
|
|