cloudy

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

0 Members and 1 Guest are viewing this topic.

Offline MCHALLIS

  • Posts: 2,102
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Sunrise - Sunset PHP Script
« 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 »

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 #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.

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 #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: [Select]
// 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


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 #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?



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 #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 »

Offline daveq

  • Posts: 3,098
  • Hollywood, MD
    • Hollywood - St. Mary's County, Maryland
Re: PHP code for day/night, sunrise/sunset times
« 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: [Select]
$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.  :oops:

--Dave

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 #6 on: May 22, 2008, 08:06:59 PM »
You're missing the putenv("TZ=$myTZ"); line

Code: [Select]
// 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.

Offline daveq

  • Posts: 3,098
  • Hollywood, MD
    • Hollywood - St. Mary's County, Maryland
Re: PHP code for day/night, sunrise/sunset times
« 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: [Select]
// 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

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 #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

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 #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.

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 #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 »

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 #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 »

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 #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

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 #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 :D

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 #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:

 

cumulus