Trixology
General Category => General Computing/Macintosh => Topic started by: TechnoMonkey on February 06, 2016, 03:02:30 PM
-
I have been unable to understand how to use the return value from the moonrise/moonset script I found. The source did not have anything but the class, no example for access.
class Moon {
/**
* Calculates the moon rise/set for a given location and day of year
*/
public static function calculateMoonTimes($month, $day, $year, $lat, $lon) {
/* for brevity, calculations were done here and left out as I do not think it pertinent to my question. */
// Convert to unix timestamps and return as an object
$retVal = new stdClass();
$utrise = self::convertTime($utrise);
$utset = self::convertTime($utset);
$retVal->moonrise = $rise ? mktime($utrise['hrs'], $utrise['min'], 0, $month, $day, $year) : mktime(0, 0, 0, $month, $day + 1, $year);
$retVal->moonset = $set ? mktime($utset['hrs'], $utset['min'], 0, $month, $day, $year) : mktime(0, 0, 0, $month, $day + 1, $year);
return $retVal;
I am able to get the info through brute force, but it seems that I should be able to use '$retVal' for a cleaner code. I just do not know how. I have tried googling php sites and all the testing I do either returns a blank page or server error.
My brute force respnse;
//should be cleaner, but works for now
$v = var_export(Moon::calculateMoonTimes($month, $day, $year, $lat, $lon), true);
$moonrise = substr($v,46,10)."<br>";
$moonset = substr($v,74,10)."<br>";
$rise = date("H:i", $moonrise);
$set = date("H:i", $moonset);
-
Hello technomonkey,
You might try posting your question/problem at [size=78%]http://www.wxforum.net (http://www.wxforum.net)[/size]. Of course, you will need an active profile to post, but no big deal.
There is an entire forum at [/size][size=78%]http://www.wxforum.net/index.php?board=87.0 (http://www.wxforum.net/index.php?board=87.0)[/size] for php and scripting and many developers who are active there.
Good luck,
Alan
-
The script returns an object (kind of array) so it needs the -> addressing
You can always use a var_dump to see the different "fields" returned in the object
Using this code with my current time/lat/lon:$var = Moon::calculateMoonTimes($month, $day, $year, $lat, $lon);
$rise = date("H:i", $var -> moonrise);
$set = date("H:i", $var -> moonset);
echo '<br />$rise = '.$rise;
echo '<br />$set = '.$set;
Returns this output:
$rise = 05:18
$set = 14:34
Hope this explains it enough/
Wim
-
Perfect. Thanks!
"$var -> moonrise" <- that was the part I was getting wrong in my attempts.
I am still a newbie dealing with objects and classes. Initially I started with php just to eliminate redundant information like menus etc. on my pages and progressed into calculations. Switch/case was great for readability of long if/else statements. Php is great. Still learning.
Thanks again.
(typos do not help either, just noticed I spelled "looking" wrong.)