Weather-Watch.com
September 02, 2010, 05:26:08 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
Members: 8,509  Posts: 362,030  Topics: 40,113
Please welcome lawrencerebeiro, our newest member.

Latest versions: WD - 10.37P  WDL - 6.05  MML - 1.03
 News:
Check out the new FAQs (Frequently Asked Questions) board for a growing list of hints, tips and diagnostic suggestions.
  Advanced Search
   Home   Bug Tracker Photo Gallery Wiki Chat Calendar Search Login Register Help  
Pages: [1] 2 3
  Print  |  « previous  |  next »  |  Go Down  
Author Topic: Sunrise - Sunset PHP Script  (Read 6396 times)
0 Members and 1 Guest are viewing this topic.
MCHALLIS
Rain Maker
***
Offline Offline

Location: Long Beach, WA USA
Station Type: Davis VP2+
Posts: 1,997
Local Time: Thursday 11:26



WWW
« 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
**
Offline Offline

Location: 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.


WWW
« 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
***
Offline Offline

Location: Long Beach, WA USA
Station Type: Davis VP2+
Posts: 1,997
Local Time: Thursday 11:26



WWW
« 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:

Code:
// 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
**
Offline Offline

Location: 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.


WWW
« Reply #3 on: May 22, 2008, 05:09:21 PM »

http://www.realweatherstation.com/FILES/daynight.php

Good now.

I guess the other way would be to get the server tz setting and adjust accordingly?


Logged

MCHALLIS
Rain Maker
***
Offline Offline

Location: Long Beach, WA USA
Station Type: Davis VP2+
Posts: 1,997
Local Time: Thursday 11:26



WWW
« 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
****
Offline Offline

Location: Hollywood, MD
Station Type: Davis VP2 w/FARS
Posts: 3,097
Local Time: Thursday 13:26



WWW
« 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


Code:
$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.  Embarassed

--Dave
Logged
niko
syzygy
Junior Climate Controller
**
Offline Offline

Location: 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.


WWW
« Reply #6 on: May 22, 2008, 08:06:59 PM »

You're missing the putenv("TZ=$myTZ"); line

Code:
// 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
****
Offline Offline

Location: Hollywood, MD
Station Type: Davis VP2 w/FARS
Posts: 3,097
Local Time: Thursday 13:26



WWW
« 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?

Code:
// 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
**
Offline Offline

Location: 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.


WWW
« 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
***
Offline Offline

Location: Long Beach, WA USA
Station Type: Davis VP2+
Posts: 1,997
Local Time: Thursday 11:26



WWW
« 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
*****
Offline Offline

Location: Mesa, AZ
Station Type: Davis Vantage Pro2 Plus W/FARS Wireless
Posts: 5,577
Local Time: Thursday 11:26


Gremlins are at work...


WWW
« 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
***
Offline Offline

Location: Long Beach, WA USA
Station Type: Davis VP2+
Posts: 1,997
Local Time: Thursday 11:26



WWW
« 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
*****
Offline Offline

Location: Mesa, AZ
Station Type: Davis Vantage Pro2 Plus W/FARS Wireless
Posts: 5,577
Local Time: Thursday 11:26


Gremlins are at work...


WWW
« 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
***
Offline Offline

Location: Long Beach, WA USA
Station Type: Davis VP2+
Posts: 1,997
Local Time: Thursday 11:26



WWW
« 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 Very Happy
Logged

niko
syzygy
Junior Climate Controller
**
Offline Offline

Location: 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.


WWW
« 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.txt


WD's sunset is correct  Wink
Logged

Pages: [1] 2 3
  Print  |  « previous  |  next »  |  Go Up  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!