Posts Tagged ‘code’

Calculating the Moon Phase

Sunday, September 7th, 2008
Posted in Web Development · Tags: ,

I was searching for a algorithm to get the phase of the moon, and unfortunately the Moon Phase Class on PHP Classes is inaccurate. With some searching I was able to find 2 sites that have code to pull it off (
Lunar Phase Calculator and Moon Phase Calculation.

I modified the latter (Moon Phase Calculation and used the first function listed on that page).

<?php
function moon_phase($year, $month, $day)
{
/*
modified from http://www.voidware.com/moon_phase.htm
*/
$c = $e = $jd = $b = 0;
if ($month < 3)
{
$year--;
$month += 12;
}
++$month;
$c = 365.25 * $year;
$e = 30.6 * $month;
$jd = $c + $e + $day - 694039.09; //jd is total days elapsed
$jd /= 29.5305882; //divide by the moon cycle
$b = (int) $jd; //int(jd) -> b, take integer part of jd
$jd -= $b; //subtract integer part to leave fractional part of original jd
$b = round($jd * 8); //scale fraction from 0-8 and round
if ($b >= 8 )
{
$b = 0;//0 and 8 are the same so turn 8 into 0
}
switch ($b)
{
case 0:
return 'New Moon';
break;
case 1:
return 'Waxing Crescent Moon';
break;
case 2:
return 'Quarter Moon';
break;
case 3:
return 'Waxing Gibbous Moon';
break;
case 4:
return 'Full Moon';
break;
case 5:
return 'Waning Gibbous Moon';
break;
case 6:
return 'Last Quarter Moon';
break;
case 7:
return 'Waning Crescent Moon';
break;
default:
return 'Error';
}
}
$timestamp = time();
echo moon_phase(date('Y', $timestamp), date('n', $timestamp), date('j', $timestamp));
?>

I haven’t been able to find out how to calculate the percentage for how lit up (full) the moon is yet though.

I converted the other code in another blog post. Calculating the Moon Phase Part 2

WordPress Recent Comments Hack

Wednesday, August 27th, 2008
Posted in Web Apps · Tags: ,

I wanted to get the most recent comments in WordPress without having to use the widgets or any plugins. When you use any of the widgets it will override the sidebar.php (although you could put what you needed in a else bracket inside sidebar.php, but I figured I’d put this hack together anyway).

This hack would go in your sidebar.php

$comment_array = $wpdb->get_results("SELECT comment_date_gmt, comment_author, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 10");
$comment_total = count($comment_array);
echo '<ul>';
for ($x = 0; $x < $comment_total; $x++)
{
echo '<li>';
echo $comment_array[$x]->comment_author . ' on ';
echo '<a href="'. get_permalink($comment_array[$x]->comment_post_ID) . '#comment-' . $comment_array[$x]->comment_ID . '">';
echo get_the_title($comment_array[$x]->comment_post_ID);
echo '</a>';
echo '</li>';
}
echo '</ul>'

A Customizable Tag Cloud For WordPress

Tuesday, August 26th, 2008
Posted in Web Apps · Tags: ,

If you’ve tried using wp_tag_cloud() as an array, you’ve noticed that all it really does is give you a string, which isn’t useful. The only way I could figure out how to get the tags to where I can format them how I want was to call get_tags() in wp-includes/category.php. You can also pass arguments to it to sort it differently. Take a look at get_terms() in wp-includes/taxonomy.php to get an idea of what you can do.

Here is a hack I put together that will output your tags as a unordered list (UL) with the total number of times the tag is used in parenthesis. You can place this code in sidebar.php

$tag_array = get_tags('orderby=count&order=DESC&number=15');
$tag_total = count($tag_array);
echo '<ul>';
for ($x = 0; $x < $tag_total; $x++)
{
echo '<li>';
echo '<a href="' . get_option('home') . '/tag/' . $tag_array[$x]->slug . '/" rel="tag">';
echo $tag_array[$x]->name;
echo '</a>';
echo ' (' . $tag_array[$x]->count . ') ';
echo '</li>';
}
echo '</ul>';

Free Weather Forecast

Tuesday, April 8th, 2008
Posted in Web Development · Tags: , , , ,

I noticed on the National Weather Service’s website they now allow you to grab the forecast by REST, whereas before they only supported SOAP requests. This makes grabbing the forecast much simpler than it was before.

If you use SOAP there are many methods to grab the data, but if you use REST there are only 2. With REST you can either use DWMLgen which lets you get a little more specific information or NDFDgenByDay which is a little simpler and lets you pick either 12 hour or 24 hour increments (so you can get each day broken in half as in day and night or the full day). The response is sent back as XML for either method so you can format the data how you like.

For the example we are doing, we are going to keep it simple and use a single location, NDFDgenByDay and a 24 hourly period. We will also set it up so that we send the longitude and latitude for Austin, TX, the current date as the start date and request 7 days worth of data.
(more…)

Last Modified Posts in WordPress

Saturday, March 8th, 2008
Posted in Web Apps · Tags: ,

Want to show the last posts you updated in WordPress? This shows the posts you went back and updated or modified, which is different than your recent posts.


<?php
query_posts('showposts=10&orderby=modified&order=DESC');
if (have_posts())
{
echo '<h3>Last Modified Posts</h3>';
echo '<ul>' . "\n";
while (have_posts()) : the_post();
echo '<li>';
echo '<a href="' . get_permalink() . '">' . the_title('','', false) . '</a>';
echo '</li>' . "\n";
endwhile;
echo '</ul>';
}
?>