Posts Tagged ‘php’

How to test for any TextLinkAds first

Sunday, April 15th, 2012
Posted in Web Development · Tags: , ,

If you run Text Link Ads with the WordPress plugin, you might run into a situation where you want to check to see if there are any ads before you output them on your site. The reason, maybe that you have some heading or decoration near them that depends on them showing up. Well, I made a little hack to do just that.

/*homepage only*/
if(is_front_page() && function_exists("tla_ads"))
//hack to see if we have any ads available
global $textlinkads_object;
if (count($textlinkads_object->ads) > 0)
//output whatever heading or decoration you want here
tla_ads();//show ads

Calculating the Moon Phase Part 2

Monday, January 4th, 2010
Posted in Web Development · Tags: ,

In an older post about Calculating the Moon Phase, I converted the some code I found to PHP. However the Lunar Phase Calculator has some more information (ecliptic latitude and longitude in degrees, the moon’s distance in Earth radii, etc.), the other one doesn’t, so I went ahead and converted it from JavaScript to PHP.

RSS Cron Job

Monday, December 14th, 2009
Posted in Web Development · Tags: ,

If you are on shared web host, you might not have the ability to run lots of cron jobs or be limited to a certain number per hour or day. You might know about the “Poor Man’s Cron Job”, which is basically not to run a task (usually caching some data or fetching a feed, api, etc.) in the background until someone visits a page. Which isn’t ideal, because often the page will be slow or sometimes it won’t be up to date until the second visit (if you run that task after outputting the cached data).

Well one way to get around this is to setup an RSS feed for the data you are caching or outputting and access it with a query string such as “?rss=2.0” or Then you can check to see if the RSS variable was passed and output a RSS feed with just enough data for a feed, no need to put sensitive data in there or anything. I would suggest putting in a ttl node in the RSS feed and set it to something the aggregators like Google Reader, should obey (that way they hit your page more or less frequently, depending on your needs). The item portion of an RSS feed only needs a title or a description, although its probably a good idea to put some kind of guid in there (check the RSS 2.0 spec for more info on creating RSS feeds).

Anyway here is some sample code. It isn’t complete but gives you an idea what I mean.

if ($_GET['rss'] == 2.0)
//create RSS 2.0 feed
header('Content-Type: text/xml');
$output = '<' . '?xml version="1.0"?' . '>' . "\n";
$output .= '<rss version="2.0">' . "\n";
$output .= '<channel>' . "\n";
//process your data and output it into RSS 2.0 format
$output .= '</channel>' . "\n";
$output .= '</rss>';
//process your data as normal

Then to make sure the page is hit often, place your RSS feed into Google Reader or Bloglines or some other RSS aggregator. Then your site will be visited often and forced to update. No one else really needs to know about your RSS feeds, unless you want them to be public as well.

phpBB 3.x RSS Hack

Monday, February 2nd, 2009
Posted in Web Apps · Tags: , ,

Where I work we use phpBB and I was surprised to see version 3 didn’t have built in RSS support. I found a mod, but it contained several files and required modifying several more files. I put together a hack that will pull the latest posts that are approved and non-reported; However, it will keep them unique to a topic. So if the last 5 posts are for a topic called “That is awesome”, it will only show a link to the last post on that topic and will look for the other latest posts from other topics.

It’s configurable with a item_limit (number of posts to pull) and word_limit (how long the description is). However the description is just the title for the last post, so it will most often be “RE: original post title” unless the user changes it when posting. I could of done another query in a loop to the table phpbb_posts and pulled out the corresponding post_text, but didn’t want to have the overhead. Although you could easily modify that in and cache the file for say 5 or 15 minutes. Think of this as a head start to get the feeds from your forum for whatever you plan to do with them. 😉


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).

function moon_phase($year, $month, $day)
modified from
$c = $e = $jd = $b = 0;
if ($month < 3)
$month += 12;
$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';
case 1:
return 'Waxing Crescent Moon';
case 2:
return 'Quarter Moon';
case 3:
return 'Waxing Gibbous Moon';
case 4:
return 'Full Moon';
case 5:
return 'Waning Gibbous Moon';
case 6:
return 'Last Quarter Moon';
case 7:
return 'Waning Crescent Moon';
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

Found two WordPress plugins that have some code you might look at for moon phase calculation: ephemeris and moon phases