What is the best method / script to echo an image after sunset and before sunrise with php.
if (date("H:i") > date_sunset(time(), SUNFUNCS_RET_STRING, 51.29, 4.49, 90.7, 2)) { $icon = "icon_night"; } else { $icon = "icon_day"; } if (date("H:i") > date_sunrise(time(), SUNFUNCS_RET_STRING, 51.29, 4.49, 90.7, 2)) { $icon = "icon_night"; } else { $icon = "icon_day"; } echo $icon;
I wanted to return a day and night icon.
You're comparing current time to each of sunrise / sunset independently meaning you'll get some odd results.
This should work for determining which one to show
$now = date("H:i");
$sunrise = date_sunrise(time(), SUNFUNCS_RET_STRING, 51.29, 4.49, 90.7, 2)
$sunset = date_sunset(time(), SUNFUNCS_RET_STRING, 51.29, 4.49,
$icon = ($now > $sunrise && $now < $sunset) ? 'icon_day' : icon_night'
// Will echo icon_day or icon_night
echo $icon;
Displaying an actual icon instead of text is a very different question. If that's what you're trying to get at more information is required (do you have icons? where are you showing them? what have you tried)