Fun with PHP

with 3 Comments

A neat little bit of code to display sunrise and sunset (the functions are built into PHP but I’m guessing many people don’t realize).

<?php
    $lat = 47.133031;
    $lon = -122.274538;
    $zenith=90+50/60;
    $offset=0;
    $location = "Puyallup, WA";
 
    $sunrise=date_sunrise(time(), SUNFUNCS_RET_TIMESTAMP, $lat, $lon, $zenith, $offset);
    $sunset=date_sunset((time() + (12 * 60 * 60)), SUNFUNCS_RET_TIMESTAMP, $lat, $lon, $zenith, $offset);
 
    $message = "<ul>";
 
    if (date("U", $sunrise) < date("U", time()))
    {
        $message .= "The sun rose today at ";
    }
    else
    {
        $message .= "The sun will rise today at ";
    }
 
    $message .= date("g:i a", $sunrise);
    if (date("U", $sunset) < date("U", time()))
    {
        $message .= " and the sun set today at ";
    }
    else
    {
        $message .= " and the sun will set today at ";
    }
 
    $message .= date("g:i a", $sunset);
 
    $message .= "</ul>";
 
    echo($message);
?>

3 Responses

  1. Sarah
    |

    You are silly! Computer geekie husband!!!!!!!

  2. Peter
    |

    An important question: “Why is it built into PHP?”

    My best guess (so far) is that the people who designed PHP were vampires.

    Or trolls.

    Or terribly romantic.

    Love ya, bro,

    -Peter

  3. Peter
    |

    Just curious – has it adapted to the new DST?
    Love,
    Peter

Leave a Reply