1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
<?php
/**
* @package Horde
*/
class Horde_Block_Moon extends Horde_Core_Block
{
/**
*/
public function __construct($app, $params = array())
{
parent::__construct($app, $params);
$this->_name = _("Moon Phases");
}
/**
*/
protected function _params()
{
return array(
'phase' => array(
'name' => _("Which phases"),
'type' => 'enum',
'default' => 'current',
'values' => array('current' => _("Current 4 Phases"),
'next' => _("Next 4 Phases"))),
'hemisphere' => array(
'name' => _("Hemisphere"),
'type' => 'enum',
'default' => 'northern',
'values' => array('northern' => _("Northern Hemisphere"),
'southern' => _("Southern Hemisphere"))),
);
}
/**
*/
protected function _content()
{
$phases = $this->_calculateMoonPhases(date('Y'));
$now = time();
$lastNew = 0;
$lastNewStamp = 0;
$offset = 0;
foreach ($phases as $key => $val) {
if ($key < $now && $key > $lastNewStamp && $val == _("New Moon")) {
$lastNew = $offset;
$lastNewStamp = $key;
}
$offset++;
}
$dates = (isset($this->_params['phase']) && ($this->_params['phase'] == 'next'))
? array_slice(array_keys($phases), $lastNew + 4, 4)
: array_slice(array_keys($phases), $lastNew, 4);
$location = (isset($this->_params['hemisphere']) && ($this->_params['hemisphere'] == 'northern'))
? _("Northern Hemisphere")
: _("Southern Hemisphere");
$html = '<table width="100%" height="100%" cellspacing="0">' .
'<tr><td colspan="4" class="control"><strong>' . $location . '</strong></td></tr>' .
'<tr height="100%"><td width="25%" align="center">' .
Horde_Themes_Image::tag('block/moon/newmoon.png', array('alt' => _("New Moon"))) .
'<br />' . strftime($GLOBALS['prefs']->getValue('date_format_mini'), $dates[0]) .
'</td>';
$html .= '<td width="25%" align="center">';
if (isset($this->_params['hemisphere']) &&
($this->_params['hemisphere'] == 'northern')) {
$html .= Horde_Themes_Image::tag('block/moon/lastquarter.png', array('alt' => _("First Quarter")));
} else {
$html .= Horde_Themes_Image::tag('block/moon/firstquarter.png', array('alt' => _("First Quarter")));
}
$html .= '<br />' . strftime($GLOBALS['prefs']->getValue('date_format_mini'), $dates[1]) . '</td>';
$html .= '<td width="25%" align="center">' .
Horde_Themes_Image::tag('block/moon/fullmoon.png', array('alt' => _("Full Moon"))) .
'<br />' . strftime($GLOBALS['prefs']->getValue('date_format_mini'), $dates[2]) . '</td>';
$html .= '<td width="25%" align="center">';
if (isset($this->_params['hemisphere']) &&
($this->_params['hemisphere'] == 'northern')) {
$html .= Horde_Themes_Image::tag('block/moon/firstquarter.png', array('alt' => _("Last Quarter")));
} else {
$html .= Horde_Themes_Image::tag('block/moon/lastquarter.png', array('alt' => _("Last Quarter")));
}
$html .= '<br />' . strftime($GLOBALS['prefs']->getValue('date_format_mini'), $dates[3]) . '</td></tr></table>';
return $html;
}
/**
* Returns an array with all the phases of the moon for a whole
* year.
*
* Based on code from
* http://www.zend.com/codex.php?id=830&single=1 by Are Pedersen.
*
* Converted from Basic by Roger W. Sinnot, Sky & Telescope, March 1985.
* Converted from javascript by Are Pedersen 2002
* Javascript found at http://www.stellafane.com/moon_phase/moon_phase.htm
*
* @param integer $year The four digit year to return the moon phases
* for.
*
* @return array The moon phases.
*/
private function _calculateMoonPhases($Y)
{
$R1 = 3.14159265 / 180;
$U = false;
$K0 = intval(($Y - 1900) * 12.3685);
$T = ($Y - 1899.5) / 100;
$T2 = $T * $T;
$T3 = $T * $T * $T;
$J0 = 2415020 + 29 * $K0;
$F0 = 0.0001178 * $T2 - 0.000000155 * $T3;
$F0 += (0.75933 + 0.53058868*$K0);
$F0 -= (0.000837 * $T + 0.000335 * $T2);
$M0 = $K0 * 0.08084821133;
$M0 = 360 * ($M0 - intval($M0)) + 359.2242;
$M0 -= 0.0000333 * $T2;
$M0 -= 0.00000347 * $T3;
$M1 = $K0 * 0.07171366128;
$M1 = 360 * ($M1 - intval($M1)) + 306.0253;
$M1 += 0.0107306 * $T2;
$M1 += 0.00001236 * $T3;
$B1 = $K0 * 0.08519585128;
$B1 = 360 * ($B1 - intval($B1)) + 21.2964;
$B1 -= 0.0016528 * $T2;
$B1 -= 0.00000239 * $T3;
for ($K9 = 0; $K9 <= 28; $K9 = $K9 + 0.5) {
$J = $J0 + 14 * $K9;
$F = $F0 + 0.765294 * $K9;
$K = $K9 / 2;
$M5 = ($M0 + $K * 29.10535608) * $R1;
$M6 = ($M1 + $K * 385.81691806) * $R1;
$B6 = ($B1 + $K * 390.67050646) * $R1;
$F -= 0.4068 * sin($M6);
$F += (0.1734 - 0.000393 * $T) * sin($M5);
$F += 0.0161 * sin(2 * $M6);
$F += 0.0104 * sin(2 * $B6);
$F -= 0.0074 * sin($M5 - $M6);
$F -= 0.0051 * sin($M5 + $M6);
$F += 0.0021 * sin(2 * $M5);
$F += 0.0010 * sin(2 * $B6 - $M6);
/* Add 1/2 minute for proper rounding to minutes per Sky &
* Tel article. */
$F += 0.5 / 1440;
$J += intval($F);
$F -= intval($F);
/* Convert from JD to Calendar Date. */
$julian = $J + round($F);
$parts = explode('/', $this->_jdtogregorian($julian));
$stamp = gmmktime(0, 0, 0, $parts[0], $parts[1], $parts[2]);
/* half K. */
if (($K9 - floor($K9)) > 0) {
if ($U) {
/* New half. */
$phases[$stamp] = _("First Half");
} else {
/* Full half. */
$phases[$stamp] = _("Last Half");
}
} else {
/* full K. */
if (!$U) {
$phases[$stamp] = _("New Moon");
} else {
$phases[$stamp] = _("Full Moon");
}
$U = !$U;
}
}
return $phases;
}
/**
* Checks if the jdtogregorian function exists, and if not calculates the
* gregorian date manually.
*
* @param int $julian The julian date.
*
* @return string m/d/Y
*/
private function _jdtogregorian($julian)
{
if (function_exists('jdtogregorian')) {
return jdtogregorian($julian);
}
// From http://php.net/manual/en/function.jdtogregorian.php
$julian = $julian - 1721119;
$calc1 = 4 * $julian - 1;
$year = floor($calc1 / 146097);
$julian = floor($calc1 - 146097 * $year);
$day = floor($julian / 4);
$calc2 = 4 * $day + 3;
$julian = floor($calc2 / 1461);
$day = $calc2 - 1461 * $julian;
$day = floor(($day + 4) / 4);
$calc3 = 5 * $day - 3;
$month = floor($calc3 / 153);
$day = $calc3 - 153 * $month;
$day = floor(($day + 5) / 5);
$year = 100 * $year + $julian;
if ($month < 10) {
$month = $month + 3;
} else {
$month = $month - 9;
$year = $year + 1;
}
return "$month/$day/$year";
}
}
|