Sunrise - Sunset PHP Script

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

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.

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

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?

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?

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

–Dave

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.

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

Your link looks OK to me now

Good day
Sunset will occur at 08:19 pm EDT

I fixed the code in my post, sorry about that, I was in a hurry and going out the door when I last uploaded.

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.

what are your settings? lat / long

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

Combine that with this and you have a sunrise, sunset lookup by zip code application :smiley:

WD’s sunset is correct :wink:

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

As you say it is now correct.

–Dave

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.

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”

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

hmmmm…

Just uncommented it out. Still reported 8pm.

Tried again with “US/Arizona” … same response.