Jump to content

increment date by one and pass it via url


kaiman

Recommended Posts

Hi All,

 

I have a rudimentary PHP page that displays a quote a day (366 quotes) from a text file. I have implemented sessions and javascript to account for the users time offset and this seems to be working well. As this is going to be for a iPhone mobile web app, I don't have to worry about DST or users having their computers time set incorrectly (the time is set automatically via cell towers by the service providers)

 

The challenge now is that I would like to be able add Next and Previous links which would allow users to view a quote of the day from past and future dates.

 

I have tried to implement it using the GET parameter like this:

 

// get day settings
$day = (int) ($_GET['day'] ? $_GET['day'] : date('z'));

// next quote link
echo $next_quote_link = '<a href="?day='.($day != 366 ? $day + 1 : 1).'">Next Quote >></a>';

// previous quote link
echo $previous_quote_link = '<a href="?day='.($day != 1 ? $day - 1 : 366).'"><<   Previous Quote</a>';

 

but it seems to just return the same quote and date. Can someone help me figure out what I've overlooked or need to do to pass the date to the script correctly?

 

Any help is appreciated

 

Thanks in advance,

 

kaiman

 

Here is the complete script so far:

 

<?php
// start session
session_start();
// get timezone offset
if(!isset($_SESSION['timezone']))
{
    if(!isset($_REQUEST['offset']))
    {
    ?>
        <script type="text/javascript">
        var d = new Date()
        var offset= -d.getTimezoneOffset()/60;
        location.href = "<?php echo $_SERVER['PHP_SELF']; ?>?offset="+offset;
        </script>
        <?php   
    }
    else
    {
        $zonelist = array('Kwajalein' => -12.00, 'Pacific/Midway' => -11.00, 'Pacific/Honolulu' => -10.00, 'America/Anchorage' => -9.00, 'America/Los_Angeles' => -8.00, 'America/Denver' => -7.00, 'America/Tegucigalpa' => -6.00, 'America/New_York' => -5.00, 'America/Caracas' => -4.30, 'America/Halifax' => -4.00, 'America/St_Johns' => -3.30, 'America/Argentina/Buenos_Aires' => -3.00, 'America/Sao_Paulo' => -3.00, 'Atlantic/South_Georgia' => -2.00, 'Atlantic/Azores' => -1.00, 'Europe/Dublin' => 0, 'Europe/Belgrade' => 1.00, 'Europe/Minsk' => 2.00, 'Asia/Kuwait' => 3.00, 'Asia/Tehran' => 3.30, 'Asia/Muscat' => 4.00, 'Asia/Yekaterinburg' => 5.00, 'Asia/Kolkata' => 5.30, 'Asia/Katmandu' => 5.45, 'Asia/Dhaka' => 6.00, 'Asia/Rangoon' => 6.30, 'Asia/Krasnoyarsk' => 7.00, 'Asia/Brunei' => 8.00, 'Asia/Seoul' => 9.00, 'Australia/Darwin' => 9.30, 'Australia/Canberra' => 10.00, 'Asia/Magadan' => 11.00, 'Pacific/Fiji' => 12.00, 'Pacific/Tongatapu' => 13.00);
        $index = array_keys($zonelist, $_REQUEST['offset']);
        $_SESSION['timezone'] = $index[0];
    }
}
// store timezone offset in session
date_default_timezone_set($_SESSION['timezone']);
?>
<html>
<head>
</head>
<body>
<?php
// display the date
echo "<h3>\n";
echo date("l, F jS, Y\n");
echo "</h3>\n";

// display the quote
$lines = file("quotes.txt");
$day = date("z");
echo "<p>“".$lines[$day]."”<p>\n";
?>
</body>
</html>

 

 

Link to comment
Share on other sites

You create $day on the line right before your output.

$day = date("z");
echo "<p>“".$lines[$day]."”<p>\n";

 

You'll need to put your code in that area that checks if a day was sent in the URL, and if not use the current day.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.