How to pass through "nighttime" status for custom icons/classes?

  • 1
  • Question
  • Updated 6 years ago
I'm using PHP to pass a class into a div based on the current conditions for my city. Echoing $icon gives me "clear" or "cloudy" but it does not include a note for whether it is after sunset or not.

What's the best way to pass through a nighttime or daytime variable through this?

$icon = $parsed_json->{'current_observation'}->{'icon'};
echo "${icon}";
Photo of jschleuss


  • 2 Posts
  • 0 Reply Likes
  • ignorant

Posted 6 years ago

  • 1
Photo of Brendan Hayes

Brendan Hayes, Official Rep

  • 962 Posts
  • 122 Reply Likes
If you know its night time, add "nt_" to the beginning of the icon, making it "nt_partlycloudy". If you don't know when the sunset/rise is, you can make the generalization 5am dawn, 9pm night. I'll add a development task to the next API round to include a night time field to be dynamic with sunset/rise so that it can be automatic.
Photo of jschleuss


  • 2 Posts
  • 0 Reply Likes
Thanks, Brendan.

I would do the assumption, but our longest day is a couple hours longer than our shortest.

I think I might have another method, but would need help. I could ping the local time and sunrise/sunset times to add the "nt_" or leave it out.

if { sunrise < current time } = add "nt_"
if { sunset > current time } then add "nt_"

How do I use the Wunderground API to grab the sunrise and sunset times? Can I get them in a military format?

Thank you!
Photo of visualweather


  • 4 Posts
  • 0 Reply Likes
The response has two fields 'icon_url' and 'icon'. 'icon_url' is full url to icon from default icon set. 'icon_url' can contains daytime and night time icons, but 'icon' contains only daytime icons. So you could use the icon_url field to determine whether it's night or day.

Also, Weather Underground does give sunrise and sunset times. You'll have to check documentation as I'm not exactly sure how to get them.