Unexpected result in PHP sunrise function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you tried messing with the GMT offset to see what happens?
woger
Forum Newbie
Posts: 13
Joined: Tue Jan 02, 2007 4:03 am

Post by woger »

Kieran Huggins wrote: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;
Sorry to annoy:

I "pasted" your edited code into my tables.

Code: Select all

echo "<font size='-4'>".list($hour,$min) = explode(':',date_sunrise(time(), SUNFUNCS_RET_STRING, -28.0361, 148.5814, 90, 10));
$sunrise = ($hour % 24).':'.$min;
echo $sunrise."</font>";
But obviously, I haven't quite got the hang of everything you've done.
Could you please point out where I went wrong?

Cheers
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

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

echo "<font size='-4'>".$sunrise."</font>";
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I'd rather cure the disease and not hide the symptoms.

see http://bugs.php.net/bug.php?id=32820
They have added two loops that take care of the problem

Code: Select all

N = (calc_sunset ? h_set : h_rise) + gmt_offset;
while (N > 24) {
	N -= 24;
}
while (N < 0) {
	N += 24;
}
(ext/php_date.c)
I don't see how php >= 5.1.2 can return 29 hours from date_sunrise()
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Well done volka. My hats off to you. That was a good find.
woger
Forum Newbie
Posts: 13
Joined: Tue Jan 02, 2007 4:03 am

Post by woger »

Everah wrote:Have you tried messing with the GMT offset to see what happens?
I did try. I worked this out with help from the net.

Code: Select all

<?php

$original_tz = getenv('TZ');

$format = "d/m/Y H:i";

// Server time:

$server_time = time();

echo 'Server time: ' . date($format, time()) . '';

// GMT time:

$gm_time = $server_time - date('Z', $server_time);

echo "GMT time: " . date($format, $gm_time) . "";
$a_time = $gm_time + date('Z', time());

//Roma
putenv('TZ=' . $original_tz);
putenv('TZ=Australia/Brisbane');


echo '<p>Australia/Brisbane: ' . date($format, $a_time) . "";


?>
But then I couldn't work that back into the sunrise "function"??
woger
Forum Newbie
Posts: 13
Joined: Tue Jan 02, 2007 4:03 am

Post by woger »

Thanks guys

I have ben trying to solve this for months.

Thanks for your persistence and patience

Cheers
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

volka wrote:I'd rather cure the disease and not hide the symptoms.

see http://bugs.php.net/bug.php?id=32820
They have added two loops that take care of the problem

Code: Select all

N = (calc_sunset ? h_set : h_rise) + gmt_offset;
while (N > 24) {
	N -= 24;
}
while (N < 0) {
	N += 24;
}
(ext/php_date.c)
I don't see how php >= 5.1.2 can return 29 hours from date_sunrise()
Oops! I have 5.1.1 installed here :-)

Here's my output:

Code: Select all

echo date_sunrise(time(), SUNFUNCS_RET_STRING, -28.0361, 148.5814, 90, 10);
echo '<br/>version: '.phpversion();

Code: Select all

29:17
version: 5.1.1
woger
Forum Newbie
Posts: 13
Joined: Tue Jan 02, 2007 4:03 am

Post by woger »

volka wrote: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)
Just to clarify - the PHP version was 5.0.4
At least that's what phpinfo tells me
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

neel_basu wrote: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
Please Tell Which Zenith is perfect for you
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

woger wrote:Just to clarify - the PHP version was 5.0.4
At least that's what phpinfo tells me
Right. The fix, according to the bug tracker, was going to be implemented in 5.1.2. So if your version is older than that, you will need to upgrade.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

My previous code will work regardless of PHP version, although it does add a little cruft.
Post Reply