A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.
It's fast and free.
processing...Retrieve Sign In
Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.
processing...Welcome to Webmonkey
- edit articles
- add to the code library
- design and write a tutorial
- comment on any Webmonkey article
Sign In Information Sent
Sunrise Sunset SMS
/skill level/
/viewed/
This is the code we used to create an SMS notification app that returns Sunrise/Sunset information to any user that requests it. The request is sent to the service from the users' phone via a shortcode. The user submits a city and a state, a ZIP code, or an exact address. We hit up a couple of public APIs to determine what the sunrise and sunset times are for that location, then wrap those times in a text message and mail it back.
Here is the full tutorial on Webmonkey: Build an SMS Notification App
The code
<?
/*** REMEMBER to use your own API key ***/
$apikey = "yourkeyhere";
// Get lat/long from Google HTTP Geocoder
$city = $_GET["city"];
$city = urlencode($city);
$geourl = "http://maps.google.com/maps/geo?q=$city&output=csv&key=$apikey";
$csvContent = get_url($geourl);
list($status, $accuracy, $lat, $long) = split(",", $csvContent);
if ($status != 200)
{
display_output("Eek! Couldn't determine where you are based on: $city");
}
else
{
// Get time zone from Earthtools
// http://www.earthtools.org/webservices.htm
$timeurl = "http://www.earthtools.org/timezone/$lat/$long";
$xmlContent = get_url($timeurl);
$xmlObject = simplexml_load_string($xmlContent);
$zone = $xmlObject->offset;
$localtime = reformat_time($xmlObject->localtime);
$dst = dst_to_number($xmlObject->dst);
// Get sunrise/sunset data from Earthtools
// http://www.earthtools.org/webservices.htm
$daymonth = date("j/n");
$sunurl = "http://www.earthtools.org/sun/$lat/$long/$daymonth/$zone/$dst";
$xmlContent = get_url($sunurl);
$xmlObject = simplexml_load_string($xmlContent);
$sunrise = reformat_time($xmlObject->morning->sunrise);
$sunset = reformat_time($xmlObject->evening->sunset);
// Print out sunrise/sunset msg
display_output("Sunrise: $sunrise\nSunset: $sunset\n\nbtw, we think it's $localtime where you are. Adjust accordingly.");
}
/*** Functions used by the rest of the app go here ***/
function display_output($content)
{
print header("Content-type: text/plain");
print $content;
exit;
}
function reformat_time($time, $fmt="g:i A")
{
return date($fmt, strtotime($time));
}
function dst_to_number($dst)
{
if ($dst == "True")
{
return 1;
}
else
{
return 0;
}
}
function get_url($url)
{
// Create cUrl object to grab XML content using passed variable $url
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$content = trim(curl_exec($c));
curl_close($c);
return $content;
}
?>
An example of the code in action
- This page was last modified 23:09, 16 September 2008.
Special Offer For Webmonkey Users
WIRED magazine:
The first word on how technology is changing our world.

