在php中递增和递减“date”变量(incrementing and decrementing a “date” variable in php)

我目前正在开发一个项目,它基本上是一个调度Web应用程序。 我有一个页面,用户输入一些将保存当天的文本。

用户还可以在下一天和前一天或使用日历进行输入。

在我的页面底部,我有两个按钮:上一天和下一天。

我希望能够在第二天点击,并在页面顶部显示第二天的日期。 一旦我在第二天的页面,如果我再次点击第二天,我应该去第二天,即两天头。 等等。 这同样适用于前一天。

所以,我需要一种方法,使用按钮在日历中连续前进和后退。

到目前为止,我可以在下一天和前一天立即前往,但我无法继续。

这些是页面底部的链接:

<a href="builder.php?day=previous" id="btn_previous_day">Previous day</a> <a href="builder.php?day=next" id="btn_next_day">Next day</a>

这就是我找到点击了哪个链接的方式。 此代码位于页面顶部

<?php $_SESSION['prev'] = 0; $_SESSION['next'] = 0; if(isset($_GET['day'])) { if( $_GET['day'] == 'previous' ) { $_SESSION['prev']--; $currentDay = date('j m o', strtotime(' - ' . $_SESSION['prev'] . ' day')); } if($_GET['day'] == 'next') { $_SESSION['next']++; $currentDay = date('j m o', strtotime(' + ' . $_SESSION['next'] . ' day')); } } else { $currentDay = date('j m o');

} ?>

这是当天输出的地方:

<p><input type="text" id="title" style="border: none;" name="title" size="20" value=" WOD <?php echo $currentDay; ?>"/></p>

谢谢!!!!

I am currently working on a project and it is basically a scheduling web app. I have one page where users enter some text that will be save for the current day.

Users are also allowed to make entries for the next and previous day, or using a calendar.

At the bottom of my page I have two buttons: previous and next day.

I would like to be able to click, for example, in the next day and display the date of the next day at the top of the page. And once I am in the page of the next day, if I click next day again I should go to the next next day, i.e., two days a head. And so on. The same would be applied to the previous day.

So, I need a way to continuously go forward and backward in the calendar using the buttons.

So far, I can go to the imediately next and previous day, but I cannot continue.

These are the links at the bottom of the page:

<a href="builder.php?day=previous" id="btn_previous_day">Previous day</a> <a href="builder.php?day=next" id="btn_next_day">Next day</a>

This is how I find which link was clicked. This code is at the top of the page

<?php $_SESSION['prev'] = 0; $_SESSION['next'] = 0; if(isset($_GET['day'])) { if( $_GET['day'] == 'previous' ) { $_SESSION['prev']--; $currentDay = date('j m o', strtotime(' - ' . $_SESSION['prev'] . ' day')); } if($_GET['day'] == 'next') { $_SESSION['next']++; $currentDay = date('j m o', strtotime(' + ' . $_SESSION['next'] . ' day')); } } else { $currentDay = date('j m o');

} ?>

And here is where a output the current day:

<p><input type="text" id="title" style="border: none;" name="title" size="20" value=" WOD <?php echo $currentDay; ?>"/></p>

Thank you!!!!

最满意答案

尝试这个:

@session_start(); if(isset($_GET['day'])) { if( $_GET['day'] == 'previous' ) { $_SESSION['currentDay']=$currentDay = date('j m o', (strtotime($_SESSION['currentDay']) - (3600*24))); } if($_GET['day'] == 'next') { $_SESSION['currentDay']= $currentDay = date('j m o', (strtotime($_SESSION['currentDay']) + (3600*24))); } } else { $_SESSION['currentDay']=$currentDay = date('j m o'); }

Just wanted to post the solution that I found to my question, based on all the suggestions made here. Thank you all.

My solution to the question:

if(!$_SESSION['dayIndex']){ $_SESSION['dayIndex'] = 0; } if(isset($_GET['day'])){ if( $_GET['day'] == 'previous' ){ --$_SESSION['dayIndex']; $currentDay = date('j m o', strtotime( $_SESSION['dayIndex'] . 'day')); } if($_GET['day'] == 'next'){ ++$_SESSION['dayIndex']; $currentDay = date('j m o', strtotime($_SESSION['dayIndex'] . ' day')); } } else { $currentDay = date('j m o'); }

更多推荐