Page 1 of 2

Unexpected result in PHP sunrise function

Posted: Tue Jan 02, 2007 4:42 am
by woger
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm using the sunrise function pretty much as per the manual.

Code: Select all

echo "<font size='-4'>".date_sunrise(time(), SUNFUNCS_RET_STRING, -28.0361, 148.5814, 90, 10) ."</font>";
But, the time it returns is 29.13, where actual sunrise is 5.13.

My server is on PST time (GMT -8) and I want sunrise for GMT +10.

It can be viewed at http://www.maranoa.org.au/xoops/index.php.

My questions are:
how do I fix it?
Some people say they see it OK - as in 5.13. Why is that?
Look forward to your reply.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jan 02, 2007 6:49 am
by volka
That's very strange. Particularly with regard to
http://www.maranoa.org.au/xoops/index.php wrote:St George 29:17 19:01
not beeing a valid date at all.

Posted: Tue Jan 02, 2007 10:33 am
by onion2k
It's 24 hours different to what you're expecting. Sounds like a date problem somewhere.

Posted: Tue Jan 02, 2007 10:52 am
by neel_basu
Make A Slight Changes On latitude and longitude
And Test It

Posted: Tue Jan 02, 2007 11:00 am
by neel_basu
IT WOULD BE THE BEST SOLUTION
========================
If the Above Try doesn't Work Choose The perfect zenith For You
By The following Codes

Code: Select all

<?php
$lat = 22.32;
$lng = 88.24;
for($i=250;$i>=80;$i--)
  {
    echo "With zenith : ".$i."-->".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $lng, $i, 5.5)."<br />\n";
  }
?>
Select The Zenith Thats Perfect For You And Use It

Posted: Tue Jan 02, 2007 3:33 pm
by woger
volka wrote:That's very strange. Particularly with regard to
http://www.maranoa.org.au/xoops/index.php wrote:St George 29:17 19:01
not beeing a valid date at all.
Volka, they are sunrise and sunset times - not dates.
The date is worked out in the actual PHP function - which is where I guess the problem is. Not in the PHP function, but in server time vs local time :)

As for Zenith, dropped the code in - and you can see the result!

Posted: Tue Jan 02, 2007 3:44 pm
by volka
woger wrote:Volka, they are sunrise and sunset times - not dates.
And what is 29 hours? am or pm?
Even a Bajoran day has only 26 hours ;)

Posted: Tue Jan 02, 2007 3:48 pm
by woger
Therein lies my problem. I was expecting 5.13.

For some reason, (becuase of timezones I expect), it's adding 24 hours to arrive at 5.13 + 24 = 29.13.

I could have tried deleting 24 from the time, but I'd like to work out why the error is occuring.

Cheers

Posted: Tue Jan 02, 2007 3:57 pm
by Kieran Huggins
Sounds like a bug.

I would use the modulo operator to fix it:

Code: Select all

$sunrise_time = date_sunrise(time(), SUNFUNCS_RET_STRING, -28.0361, 148.5814, 90, 10) % 24;

Posted: Tue Jan 02, 2007 4:03 pm
by woger
Kieran Huggins wrote:Sounds like a bug.

I would use the modulo operator to fix it:

Code: Select all

$sunrise_time = date_sunrise(time(), SUNFUNCS_RET_STRING, -28.0361, 148.5814, 90, 10) % 24;
I had seen info about a bug when the time difference was a certian amount, but I'm a newb!!!

Used the modulo thingy - it now returns 5, rather than 5.17.
Can I fix that?

Cheers

Posted: Tue Jan 02, 2007 4:07 pm
by Kieran Huggins
brain fart.... please hold.

Posted: Tue Jan 02, 2007 4:09 pm
by volka
hm, what php version do you actually use?
Your webserver claims
Server: Apache/2.0.47 (Fedora)
X-Powered-By: PHP/4.3.8
But php4 does not support date_sunrise. Did you install e.g. a pecl extension?

http://www.php.net/ChangeLog-5.php
Version 5.1.2
12-Jan-2006
...
Fixed bug #32820 (date_sunrise and date_sunset don't handle GMT offset well). (Derick)
...
Fixed bug #30937 (date_sunrise() & date_sunset() don't handle endless day/night at high latitudes). (Derick)

Posted: Tue Jan 02, 2007 4:22 pm
by Kieran Huggins
I'm getting the same behaviour with PHP 5.1.2

This code will work, though it's less awesome than I hoped :-(

Code: Select all

list($hour,$min) = explode(':',date_sunrise(time(), SUNFUNCS_RET_STRING, -28.0361, 148.5814, 90, 10));
$sunrise = bcmod($hour,'24').':'.$min;

Posted: Tue Jan 02, 2007 4:46 pm
by woger
Kieran Huggins wrote:I'm getting the same behaviour with PHP 5.1.2

This code will work, though it's less awesome than I hoped :-(

Code: Select all

list($hour,$min) = explode(':',date_sunrise(time(), SUNFUNCS_RET_STRING, -28.0361, 148.5814, 90, 10));
$sunrise = bcmod($hour,'24').':'.$min;
I get an error:
Fatal error: Call to undefined function bcmod() in
Perhaps it's the version of PHP - 5.0.x I think

Posted: Tue Jan 02, 2007 4:59 pm
by Kieran Huggins
you don't need bcmod after all, I just left it from earlier experimenting:

Code: Select all

list($hour,$min) = explode(':',date_sunrise(time(), SUNFUNCS_RET_STRING, -28.0361, 148.5814, 90, 10));
$sunrise = ($hour % 24).':'.$min;