Jump to content

Previous Day & Next Day


pghutch

Recommended Posts

I have a page where the day and date is displayed like this.

 

 

< Previous Day - Wednesday January 23, 2013 > Next Day

 

 

How do I make it so when the userclicks on < Previous Day link that they go to Tusday January 22, 2013 and when they lick on > Next Day they go to Thursday January 24, 2013 etc?

Link to comment
https://forums.phpfreaks.com/topic/273553-previous-day-next-day/
Share on other sites

Well I am pretty new to PHP So any other code will hep me.

 

Ive started with this but how do I add code to the actual

labels here

 

< Previous Day - Wednesday January 23, 2013 > Next Day

 

What code goes on < Previous Day and > Next Day?

 

Heres what I have so far

 

<?php

$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');

$previous_date = date('Y-m-d', strtotime($date .' -1 day'));

$next_date = date('Y-m-d', strtotime($date .' -1 day'));

?>

So I now have this. But the links don't go to the next or previous day.

 

The links look like this /index.php?date=<?=$next_date;?>.

 

Where they should actually pass a actual date instead of "<?=$next_date;?>."

 

<?php

$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');

$previous_date = date('Y-m-d', strtotime($date .' -1 day'));

$next_date = date('Y-m-d', strtotime($date .' -1 day'));

?>

 

<a href="?date=<?=$previous_date;?>">Previous</a>

<a href="?date=<?=$next_date;?>">Next</a>

OK here is where I am now. I appreciate all of the help.

But still no dates are passed in the url....its blank, like this.

 

The links look like this..../index.php?date=

 

Heres my code

 

<?php

$date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');

$previous_date = date('Y-m-d', strtotime($date .' -1 day'));

$next_date = date('Y-m-d', strtotime($date .' +1 day'));

?>

 

 

 

<a href="index.php?<?php echo date?>=<?php echo $row->$previous_date;?>" > Previous </a>;?> Todays Date <a href="index.php?<?php echo date?>=<?php echo $row->$next_date;?>" > Next </a>;?>

Well thanks to everyone's help I've gotten the previous and next to work.

But I have one last question.

I want my date formatted like this: Wednesday January 23, 2012

Right now with the code below the date is outputting like this 2013-01-28.

 

How can I output this <span class="datetxt"> <?php echo $_GET['date'];?></span> to this Wednesday January 23, 2012

 

Here is the code.

 

 

<?php echo '<h3 class="home-title">Schedule for Today<span class="date">';?>

 

<a href="index.php?<?php echo date?>=<?php echo $previous_date;?>" > <img src="assets/images/lftarrow.png" width="28" height="12"> </a>

<span class="datetxt"> <?php echo $_GET['date'];?></span>

<a href="index.php?<?php echo date?>=<?php echo $next_date;?>" ><img src="assets/images/rtarrow.png" width="28" height="12"></a> </span></h3>

I would suggest that instead of passing strings such as "2012-01-05" as the parameter on the URL - just send an offset from the current date (i.e. +5). It will make your code cleaner and validation simpler (which you are not doing now). It also helps in that you don't have to manipulate a lot of strings. Keep data in as simple/agnostic a format as possible until needed.

 

<?php
//Get offset from URL parameter, default is 0
$offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
//Define display date using current date and the offset
$display_date = date('l F j, Y', strtotime("$offset days"));
//Determine prev/next offsets based on current offset
$prev_off = $offset - 1;
$next_off = $offset + 1;

?>

<h3 class="home-title">
   Schedule for Today
   <span class="date">
       <a href="test.php?offset=<?php echo $prev_off; ?>"><img src="assets/images/lftarrow.png" width="28" height="12"></a>
       <span class="datetxt"><?php echo $display_date;?></span>
       <a href="test.php?offset=<?php echo $next_off; ?>"><img src="assets/images/rtarrow.png" width="28" height="12"></a>
   </span>
</h3> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.