Rocco The Taco
Rocco The Taco

Reputation: 3777

Simulate PHP Date N forward and backward using PHP

I needed to display different text based on the day of the week and successfully created the following code snippet using if/else. Now my client wants forward/back arrows to simulate going forwards and backwards from the current day of the week. Is there a way to enhance this script to _get a value in the URL and +1 or -1 of the $today variable?

        <?php $today = date('N');
if( $today == 7) { ?>
<th class="WADAResultsTableHeader">Sunday</th>
<?php } elseif( $today == 1 ) { ?>
<th class="WADAResultsTableHeader">Monday</th>
<?php } elseif( $today == 2 ) { ?>
<th class="WADAResultsTableHeader">Tuesday</th>
<?php } elseif( $today == 3 ) { ?>
<th class="WADAResultsTableHeader">Wednesday</th>
<?php } elseif( $today == 4 ) { ?>
<th class="WADAResultsTableHeader">Thursday</th>
<?php } elseif( $today == 5 ) { ?>
<th class="WADAResultsTableHeader">Friday</th>
<?php } elseif( $today == 6 ) { ?>
<th class="WADAResultsTableHeader">Saturday</th>
<?php } ?>

Upvotes: 0

Views: 197

Answers (3)

SyntaxLAMP
SyntaxLAMP

Reputation: 985

Using a hyperlink you can do:

echo "<a href=\"page.php?today=".$today-1."\">Back</a>";

And then to retrieve the today variable:

$today=$_GET["today"];

Upvotes: 0

blots
blots

Reputation: 201

This is a working tested example

if (array_key_exists('day', $_GET)) {
    $add_day = $_GET['day'];
} else {
    $add_day = 0;
}

$yesterday = $add_day - 1;
$tomorrow = $add_day + 1;

$link_yesterday = $_SERVER['PHP_SELF'] . '?day=' . $yesterday ;
$link_tomorrow  = $_SERVER['PHP_SELF'] . '?day=' . $tomorrow ;


$display_day = mktime(0, 0, 0, date("m")  , date("d") + $add_day, date("Y"));


$today = date('N', $display_day);

?>
<table>
<tr>

<?php if( $today == 7) { ?>
<th class="WADAResultsTableHeader">Sunday</th>
<?php } elseif( $today == 1 ) { ?>
<th class="WADAResultsTableHeader">Monday</th>
<?php } elseif( $today == 2 ) { ?>
<th class="WADAResultsTableHeader">Tuesday</th>
<?php } elseif( $today == 3 ) { ?>
<th class="WADAResultsTableHeader">Wednesday</th>
<?php } elseif( $today == 4 ) { ?>
<th class="WADAResultsTableHeader">Thursday</th>
<?php } elseif( $today == 5 ) { ?>
<th class="WADAResultsTableHeader">Friday</th>
<?php } elseif( $today == 6 ) { ?>
<th class="WADAResultsTableHeader">Saturday</th>
<?php } ?>

</tr>
</table>

<a href="<?php echo $link_yesterday;?>">Previous Day</a>
<a href="<?php echo $link_tomorrow;?>">Next day</a>

Upvotes: 1

user3562712
user3562712

Reputation:

Why don't you just make an array of days?

<?php
    //If day is set, use it. Else, use the current day.
    $today      = (isset($_GET['day']) ? $_GET['day'] : date("N") - 1); 
    $tomorrow   = $today + 1;
    $yesterday  = $today - 1;
    $yest_link  = "$_SERVER[PHP_SELF]?day=$yesterday";
    $today_link = "$_SERVER[PHP_SELF]?day=$today";
    $tom_link   = "$_SERVER[PHP_SELF]?day=$tomorrow";

    $days = [
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday"
    ]
?>
<table>
<th class="WADAResultsTableHeader">
    <a href=<?= $yest_link ?>>
        <?= $days[$yesterday] ?>
    </a>
</th>
<th class="WADAResultsTableHeader">
    <a href=<?= $today_link ?>>
        <?= $days[$today] ?>
    </a>
</th>
<th class="WADAResultsTableHeader">
    <a href=<?= $tom_link ?>>
        <?= $days[$tomorrow] ?>
    </a>
</th>
</table>

Upvotes: 1

Related Questions