Git Product home page Git Product logo

php-moon-phase's Introduction

PHP from Packagist Codacy Badge Latest Stable Version Total Downloads License

Solaris PHP Moon Phase

Calculate the phases of the Moon in PHP. This library is based on Moontool for Windows.

Installation

This library is made for the use with Composer. Add it to your project by running $ composer require solaris/php-moon-phase.

Usage

Create an instance of the MoonPhase class, supplying a DateTime object with a UNIX timestamp for when you want to determine the moon phase (if you don't then the current time will be used).

You can then use the following methods:

  • getPhase(): the terminator phase angle as a fraction of a full circle (i.e., 0 to 1). Both 0 and 1 correspond to a New Moon, and 0.5 corresponds to a Full Moon.
  • getIllumination(): the illuminated fraction of the Moon (0 = New, 1 = Full).
  • getAge(): the age of the Moon, in days.
  • getDistance(): the distance of the Moon from the centre of the Earth (kilometres).
  • getDiameter(): the angular diameter subtended by the Moon as seen by an observer at the centre of the Earth (degrees).
  • getSunDistance(): the distance to the Sun (kilometres).
  • getSunDiameter(): the angular diameter subtended by the Sun as seen by an observer at the centre of the Earth (degrees).
  • getPhaseNewMoon(): the time of the New Moon in the current lunar cycle, i.e., the start of the current cycle (UNIX timestamp).
  • getPhaseNextNewMoon(): the time of the New Moon in the next lunar cycle, i.e., the start of the next cycle (UNIX timestamp).
  • getPhaseFullMoon(): the time of the Full Moon in the current lunar cycle (UNIX timestamp).
  • getPhaseNextFullMoon(): the time of the Full Moon in the next lunar cycle (UNIX timestamp).
  • getPhaseFirstQuarter(): the time of the first quarter in the current lunar cycle (UNIX timestamp).
  • getPhaseNextFirstQuarter(): the time of the first quarter in the next lunar cycle (UNIX timestamp).
  • getPhaseLastQuarter(): the time of the last quarter in the current lunar cycle (UNIX timestamp).
  • getPhaseNextLastQuarter(): the time of the last quarter in the next lunar cycle (UNIX timestamp).
  • getPhaseName(): the phase name.

Example

<?php

use Solaris\MoonPhase;

$moonPhase = new MoonPhase();

$age = round($moonPhase->getAge(), 1);
$stage = $moonPhase->getPhase() < 0.5 ? 'waxing' : 'waning';
$distance = round($moonPhase->getDistance(), 2);
$next = gmdate('G:i:s, j M Y', (int) $moonPhase->getPhaseNextNewMoon());

echo 'The moon is currently ' . $age . ' days old, and is therefore ' . $stage . '. ';
echo 'It is ' . $distance . ' km from the centre of the Earth. ';
echo 'The next new moon is at ' . $next . '. ';

Help

If you have any questions, feel free to contact us under [email protected].

Further information about Bit&Black can be found under www.bitandblack.com.

php-moon-phase's People

Contributors

baxevanis avatar ganstaz avatar m-a-x-s-e-e-l-i-g avatar moskito89 avatar rootshell-coder avatar solarissmoke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-moon-phase's Issues

Incorrect date format

Eample: The next first quarter is at 12:00:00, 24 Nov -4713

Bad date in:
get_phase('first_quarter')
get_phase('next_first_quarter')

Thanks,
Keep up the good work

($date) must be of type ?DateTime, bool given

Hi there,

The past version allowed us to set custom dates/times by using a format such as

#28
$moon = new Solaris\MoonPhase(strtotime(date('Y-m-d H:i')));

However after upgrading I get the following error

Uncaught exception 'TypeError' with message 'Solaris\MoonPhase::__construct(): Argument #1 ($date) must be of type ?DateTime, bool given

Cheers

Different Time Zone

I am living in UTC+1 in Winter and UTC+2 in Summer. Is there a way to set different timezones? Because atm all the outputs are 1 hour off :-(
And is there a way to set different locations?

give the moon phase for a month

Hi, thnak you for this script.

it would be nice if you could give all the phases of the moon for a month.
We enter the year and the month and the script give us an array with all the dates and phases.

Custom time

How can we set custom times? In the old version I did this, however after upgrading to the latest version it doesnt seem to be working anymore and uses the server timestamp

$moon = new Solaris\MoonPhase(strtotime(date('Y-m-d 12:00').' +1 day'));
$moon = new Solaris\MoonPhase(strtotime(date('Y-m-d 12:00').' +2 day'));
$moon = new Solaris\MoonPhase(strtotime(date('Y-m-d 12:00').' +3 day'));

Undefined method get()

$moon = new Solaris\MoonPhase();
echo '<pre>'; var_dump($moon->get('sundistance')); echo '</pre>';

That code returns Fatal error: Uncaught Error: Call to undefined method Solaris\MoonPhase::get() in [my file on line 8]. Other methods like phase() do work. Please fix :)

Moon Emoji 🌘

For my own use I added this function, maybe other like it as well!

	function phase_emoji() {
		$names = array( '🌑','🌒','🌓','🌔','🌕','🌖','🌗','🌘');
		// There are eight phases, evenly split. A "New Moon" occupies the 1/16th phases either side of phase = 0, and the rest follow from that.
		return $names[ floor( ( $this->phase + 0.0625 ) * 8 ) ];
	}

wrong times

compared to the wikipedia lunar calender your moon phases for 2016 are always a bit off (~30 to 50 minutes). That results in wrong dates in some cases.

The distance seems to me is wrong

When setting a specific date, the distance does not correspond to those searched on the internet.

I'm using the code below

function LuasDoMes($mes,$ano){
	$date = new DateTime();
	$date->setDate($ano,$mes,1);
	$date->setTime (14,48,35);
	$unix = $date->format('U');
	$moon = new Solaris\MoonPhase($unix);
	$Luas = array();
	if (gmdate('m', $moon->get_phase('new_moon'))==$mes){
		$Luas += [gmdate('d/m/Y H:i:s', $moon->get_phase('new_moon'))=>'Nova '.round($moon->get('distance'), 2).''];
	}
	if (gmdate('m', $moon->get_phase('next_new_moon'))==$mes){
		$Luas += [gmdate('d/m/Y H:i:s', $moon->get_phase('next_new_moon'))=>'Nova '.round($moon->get('distance'), 2).''];
	}
	if (gmdate('m', $moon->get_phase('full_moon'))==$mes){
		$Luas += [gmdate('d/m/Y H:i:s', $moon->get_phase('full_moon'))=>'Cheia aproximação '.round($moon->get('distance'), 2).''];
	}
	if (gmdate('m', $moon->get_phase('next_full_moon'))==$mes){
		$Luas += [gmdate('d/m/Y H:i:s', $moon->get_phase('next_full_moon'))=>'Cheia aproximação '.round($moon->get('distance'), 2).''];
	}
	if (gmdate('m', $moon->get_phase('first_quarter'))==$mes){
		$Luas += [gmdate('d/m/Y H:i:s', $moon->get_phase('first_quarter'))=>'Crescente '.round($moon->get('distance'), 2).''];
	}
	if (gmdate('m', $moon->get_phase('next_first_quarter'))==$mes){
		$Luas += [gmdate('d/m/Y H:i:s', $moon->get_phase('next_first_quarter'))=>'Crescente '.round($moon->get('distance'), 2).''];
	}
	if (gmdate('m', $moon->get_phase('last_quarter'))==$mes){
		$Luas += [gmdate('d/m/Y H:i:s', $moon->get_phase('last_quarter'))=>'Minguante '.round($moon->get('distance'), 2).''];
	}
	if (gmdate('m', $moon->get_phase('next_last_quarter'))==$mes){
		$Luas += [gmdate('d/m/Y H:i:s', $moon->get_phase('next_last_quarter'))=>'Minguante '.round($moon->get('distance'), 2).''];
	}
	ksort($Luas);
	return $Luas;
}
echo "<p>Em março de 2020 hoveram as luas<br>";
$Luas = LuasDoMes(3,2020);
foreach ($Luas as $lua => $dia) {
	echo "$lua -> $dia<br>";
}

The result obtained for March 9, 2020 at 14:48:35 was 395814

However on other sites like https://www.calendario-365.com.br/lua/fases-da-lua.html the result they present is 363972

It's a big difference and on that day it was super moon so the result of the calendario-365.com.br site is more likely to be correct

Wrong next full moon

Hi!
I noticed that some times your script returns wrong next full moon.

Let's say we have a timestamp 1062009900. Which is Aug 27, 2003.

The moon phase for this date is New Moon and it's correct. But the result of the next full moon is 1065770899, which is Oct 10, 2003. And it makes it 43 days till the next full moon.

What about geo data?

Hello!
If I calculate data in Europe and if I calculate in Australia (for same local time as in Europe) moon days and phases should be different. Any chance to include geo data to Your script?

PHP Parse error

Hi,
I use this script for years to get time of the next new moon.
I tried to update to latest version and I get this error :
Parse error: syntax error, unexpected ':', expecting ';' or '{' in MoonPhase.php on line 149
I run this Under EasyPHP with PHP version is 5.5.8 and online 5.6.34 whereas composer.json require php 5.3.0.
Any Idea ?

Different Time

How can I set a different date and time for the calculation? ATM I define
$date = time();

But it doesn't make any difference. How do I give the script another unixtime for calculation?

Script fails on live server with arse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in

parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in moon-phase.php line 10.

I had to rename the file to get it working on my 1and1 server to moon-phase.php5.

I call it via the following wrapper.

file:moonphases-sb.php5

age(), 1 ); $stage = $moon->phase() < 0.5 ? 'Waxing' : 'Waning'; $distance = round( $moon->distance(), 2 ); $miles = round(($distance * 60 * 1.1515)); ``` // $next = gmdate( 'j M Y,G:i:s', $moon->next_new_moon() ); $next = gmdate( 'j M Y', $moon->next_new_moon() ); echo "
    "; echo "
  • Current Moon is: $age days old
  • "; echo "
  • Current Moon is: $stage.
  • "; echo "
  • Next New moon is $next
  • "; echo "
"; ?>

Here is the html I use.

      <article>
        <h2>Current Moon Information</h2>
        <?php include ('../../include/moonphases-sb.php5'); ?>
     </article>

It works correctly on my windows 7 machine using WAmpServer 2.2 with PHP version 5.3.13

But when I try to run it on my hosting server with the following PHP version 5.2.17

I get the above error, have you any idea why searching on google points me to a PHP 4 issue, hence my rename of the script to ensure that works under php5

Add Images

Would be great if this could be expanded to pick an image from a local set of images and display the phase as an image along with the information currently provided.

Next New Moon, .......

Thanks for the good script.
However, I verified this:
Example
Date selected: 02/01/2017 11:33:58 am.
Result:
-next full moon: 14:54:49, 12 Mar 2017.
-next new moon: 15:00:15, 26 Feb 2017....OK
-next first quarter moon: 11:33:38, 5 Mar 2017.
-next last quarter moon: 16:01:29, 20 Mar 2017.

But the right result should be:

  • next full moon: 0:33:58, 11 Feb 2017.
  • next first quarter moon: 4:19:47, 4 Feb 2017.
  • next last quarter moon: 19:35:13, 18 Feb 2017.

Please create new release

Any chance of creating a release for the latest change merged back in 2015 - installing via Composer wont pick up this code currently. Thanks for the library by the way!

Full Moon Name (enhancement)

This would be a nice enhancement: The Name of the Full Moon for that Month.

function full_moon_name($month) {
        /* Array keyed start at 1 for January */
        $monthly_names = array( 1 => 'Moon after Yule','Snow Moon','Sap Moon','Grass Moon','Planting Moon','Honey Moon','Thunder Moon','Grain Moon','Fruit Moon','Hunter\'s Moon','Frosty Moon','Moon before Yule' );
        $month = empty($month) ? date(F) : $month;
        return $monthly_names[date(n, strtotime($month))];
    }
$full = gmdate('G:i:s, j M Y', $moon->next_full_moon());
echo "This month's full moon is called " . $moon->full_moon_name() . ".
\n"; echo "The next full moon is at $full called " . $moon->full_moon_name($full) . ".
\n";

This month's full moon is called Fruit Moon.
The next full moon is at 12:05:59, 27 Oct 2015 called Hunter's Moon.

accuracy of eccentricity integration

about epsilon in your kepler() function:
$epsilon = pow(1, -6);

you are aware that whatever exponent you pick, 1^N will always be 1.
1st run: delta = 0.0005205388763922514
-> returning 6.251514456759228

you prolly meant 1e-6 = 1*10^6 = pow(10,-6) = 0.000001
1st run: delta = 0.0005205388763922514
2nd run: delta = -7.335376750461364e-11
-> returning 6.251514456833828

hope to help, cheers!

Typo in example code

$age = round(moon->get('age'), 1); should be
$age = round($moon->get('age'), 1);

phase_name seems wrong

Hi
I'm using the phase_name function and this returns a wrong name, I'm always getting a day or two lag from the correct phase name.

setlocale(LC_TIME, "fr_FR");
date_default_timezone_set('Europe/Paris');
$moon = new Solaris\MoonPhase();
$moonphasename = $moon->phase_name();

if ($moonphasename == "New Moon"):  
....
elseif ($moonphasename == "Full Moon"):
....

I'm using this method because I have build a precise SVG drawing (from this thread) of the moon according to the moon illumination :

$mooncalc3 = $moon->get('illumination') - 0.5;
elseif ($moonphasename == "Waxing Gibbous"):  // Waxing Gibbous OK
     echo '
    <svg xmlns="http://www.w3.org/2000/svg" width="139" height="134">
      <defs>
        <mask id="phase-mask" maskContentUnits="objectBoundingBox">
          <rect x="0.5" y="0" width="0.5" height="1" fill="white"/>
          <ellipse id="phase-ellipse" cx="0.5" cy="0.5" rx="'.$mooncalc3.'" ry="0.5" fill="white"/>
        </mask>
      </defs>
      <circle fill="black" r="58" cy="69" cx="69"/><!-- moon shadow -->
      <circle fill="#fff" r="57.25" cy="69" cx="69" mask="url(#phase-mask)"/><!-- sunlight on moon -->
    </svg>';
    echo "<div>Lune: " . round($percent * 100, 1) . "&thinsp;%</div>";

Do you know a better way to check the phase name of the day ? Is it a bug ?
Thanks

Returns past phases for some timestamps

$date = new DateTime('2016-08-02 22:50:09');
$moon = new Solaris\MoonPhase($date->getTimestamp());
$timeFmt = 'Y-m-d H:i:s';

echo 'TS '.$date->getTimestamp()."\n";
echo 'Now '.date($timeFmt, $date->getTimestamp())."\n";
echo 'FQ '.date($timeFmt, $moon->first_quarter())."\n";
echo 'FM '.date($timeFmt, $moon->full_moon())."\n";
echo 'LQ '.date($timeFmt, $moon->last_quarter())."\n";
echo 'NM '.date($timeFmt, $moon->new_moon())."\n";
echo 'NNM '.date($timeFmt, $moon->next_new_moon())."\n";

---- Output: -------------------------------
TS 1470171009
Now 2016-08-02 22:50:09
FQ 2016-07-12 02:52:41
FM 2016-07-20 00:59:36
LQ 2016-07-27 01:02:35
NM 2016-07-04 13:03:23
NNM 2016-08-02 22:46:40

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.