Solved

Moon Phase

Posted on 2010-09-14
5
739 Views
Last Modified: 2012-05-10
I'd like to modify this moon phase script to replace New Moon, Waxing Crescent Moon, Quarter Moon, Waxing Gibbous Moon, Full Moon, Waning Gibbous Moon, Last Quarter Moon and Waning Crescent Moon with an image to represent the current moon phase.

Example:
case 0:
            return 'images/newmoon.gif';
            break;

Thanks
<?php
function moon_phase($year, $month, $day)
{
	$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));
?>

Open in new window

0
Comment
Question by:Michael
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:ronan1979
ID: 33675425
your example is correct

 what is your issue?
0
 
LVL 4

Expert Comment

by:ronan1979
ID: 33675484
then

<?php

$img_path = moon_phase(date('Y', $timestamp), date('n', $timestamp), date('j', $timestamp));
?>

<img src="<?php echo $img_path?>" />
0
 
LVL 2

Author Comment

by:Michael
ID: 33675498
The image does not appear, instead it returns copy that reads as:

images/newmoon.gif
0
 
LVL 4

Accepted Solution

by:
ronan1979 earned 50 total points
ID: 33675512
insert the path in a html tag : <img src="img_path" />
0
 
LVL 2

Author Closing Comment

by:Michael
ID: 33675594
Thanks for the help!!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question