Jump to content

previous and next buttons to increment/decrement date range by 1 week


Go to solution Solved by mac_gyver,

Recommended Posts

Hi All, I am having trouble trying to get the previous/ next buttons to change the date range by which items are retrieved from the database using PHP. The screen shot shows the 7 cells that I want to change when I click on either the next or previous button. The first date in the cells are determined using Datetime "Monday this week". I then use a +1 loop to get the following cells populated. I'd like to change this range by plus/minus 1 week at each push of the next/previous button.

The code is below:

  $dw = new DateTime('monday this week', new DateTimeZone($tz));
        $fdw = $dw->format('l j M');

The code below that I have been using currently is working but only once; ie: I can only either go back 1 week or ahead of this week 1 week. Another button push doesn't work.

if(isset($_POST['next'])) {
$dw = new DateTime($fdw);
$nw = $dw->modify('+1 week');
$nfdw = $nw->format('l j M');
$fdw = $nfdw;
}
if(isset($_POST['prev'])) {
$dw = new DateTime($fdw);    
$pw = $dw->modify('-1 week');
$pfdw = $pw->format('l j M');
$fdw = $pfdw;
}

What I can't seem to get it to do is reassign $fdw to the new value, so when the button is pressed again, it calculates from that value rather than this week.

I hope that makes sense? If anyone could help with this that would be very much appreciated. 

Screenshot 2025-01-19 214319.png

32 minutes ago, Plugnz13 said:

Hi All, I am having trouble trying to get the previous/ next buttons to change the date range by which items are retrieved from the database using PHP. The screen shot shows the 7 cells that I want to change when I click on either the next or previous button. The first date in the cells are determined using Datetime "Monday this week". I then use a +1 loop to get the following cells populated. I'd like to change this range by plus/minus 1 week at each push of the next/previous button.

The code is below:

  $dw = new DateTime('monday this week', new DateTimeZone($tz));
        $fdw = $dw->format('l j M');

The code below that I have been using currently is working but only once; ie: I can only either go back 1 week or ahead of this week 1 week. Another button push doesn't work.

if(isset($_POST['next'])) {
$dw = new DateTime($fdw);
$nw = $dw->modify('+1 week');
$nfdw = $nw->format('l j M');
$fdw = $nfdw;
}
if(isset($_POST['prev'])) {
$dw = new DateTime($fdw);    
$pw = $dw->modify('-1 week');
$pfdw = $pw->format('l j M');
$fdw = $pfdw;
}

What I can't seem to get it to do is reassign $fdw to the new value, so when the button is pressed again, it calculates from that value rather than this week.

I hope that makes sense? If anyone could help with this that would be very much appreciated. 

Screenshot 2025-01-19 214319.png

You need to ensure that $fdw is initialized properly before it is used.

$fdw is expected to hold a date string, but if it is not initialized before the form is submitted, the DateTime constructor will throw an error.

What does your error logs say?

  • Solution

you are doing 'date-ination'. it's like pagination, but using dates. you should be using a get request to determine what will be displayed on the page. this is so that if someone finds a result they would like to return to or share, they can bookmark or share the URL and can return to the same result. the dates you pass in the URL should be a standard YYYY-MM-DD format. format the dates as 'l j M' only when you display them. you would default to the current monday if there is no get input. you would produce the previous/next links with the previous/next monday's date and include any existing get parameters so that if you add other search/filters, they will automatically get propagated in the URL between pages.

example code -

<?php

date_default_timezone_set('America/Denver');

// default to the current monday if there is no get input
if(!isset($_GET['fdw']))
{
	$dw = new DateTime('monday this week');
	$fdw = $dw->format('Y-m-d');
}
else
{
	// you should validate that the get input is a properly formatted date - code left up to you
	$fdw = $_GET['fdw'];
}


// use $fdw in your code to produce the output
$dw = new DateTime($fdw);
echo $dw->format('l j M') . '<br>';


// get a copy of any existing get parameters
$get = $_GET;

// produce the previous link
// calculate previous date
$dw = new DateTime($fdw);
$pw = $dw->modify('-1 week');
$pfdw = $pw->format('Y-m-d');

// set the fdw element
$get['fdw'] = $pfdw;

// build the query string part of the url
$qs = http_build_query($get,'','&amp;');
echo "<a href='?$qs'><button>< Previous Week</button></a>";

// produce the next link
// calculate next date
$dw = new DateTime($fdw);
$nw = $dw->modify('+1 week');
$nfdw = $nw->format('Y-m-d');

// set the fdw element
$get['fdw'] = $nfdw;

// build the query string part of the url
$qs = http_build_query($get,'','&amp;');
echo "<a href='?$qs'><button>Next Week ></button></a>";


 

  • Like 1
  • Great Answer 1

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.