使用 strtotime() 推进相对日期
我正在尝试使用 strtotime()
响应按钮单击,相对于前一次单击的日期提前 -1 和 +1 天(两个按钮)。
示例:
- 现在是该月的 10 号,我点击“-1 天”按钮,现在日期显示为 9 号。
- 我再次单击“-1 天”按钮,现在读数显示为第 8 天。
- 我单击“+1 天”按钮,现在读数显示这是 9 号。
我了解按钮和显示日期以及使用 $_GET
和 PHP 传递信息,但是如何让 strtotime()
处理上次的相对日期时间旅行脚本被调用的时间?
到目前为止,我的工作让我能够展示相对于现在的昨天和今天,但不相对于例如前天或后天< /em>。 或者,如果我使用“上周一”按钮,则表示当天的前一天或后一天。
I'm trying to use strtotime()
to respond to a button click to advance -1 and +1 days (two buttons) relative to the day advanced to on the previous click.
Example:
- It's the 10th of the month, I click "-1 day" button, and now the date reads as the 9th.
- I click the "-1 day" button again and now the readout states the 8th day.
- I click the "+1 day" button and now the readout states it's the 9th.
I understand the buttons and the displaying the date and using $_GET
and PHP to pass info, but how do I get strtotime()
to work on the relative date from the last time the time travel script was called?
My work so far has let me show yesterday and today relative to now but not relative to, for example, the day before yesterday, or the day after tomorrow. Or if I use my "last monday" button, the day before or after whatever that day is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
凯文,你有一个坚实的绝对基础(即日期/时间),而不是相对时间段。 然后您可以转换为相对时间段。 因此,例如,默认情况下,如果您显示日历,则将从今天开始工作。
您可以在 strtotime 的函数定义中看到,第二个参数是 now,即您可以更改它的相对日期。
通过快速循环可能会更容易显示
这将使用“昨天”作为第一个参数循环显示过去 10 天。 然后我们使用日期将其打印出来。
因此,请通过 URI 传递时间/日期,以便保存相对日期。
Kevin, you work off a solid absolute base (i.e. a date / time), not a relative time period. You then convert to the relative time periods. So, for example, by default, if you were showing a calendar, you'd work from todays date.
You can see in the function definition here of strtotime, the second argument is now, i.e. you can change the date from which it's relative.
This might be easier to display through a quick loop
This will loop through the last 10 days using "yesterday" as the first argument. We then use date to print it out.
So pass the time/date in via the URI so you can save the relative date.
经过片刻的灵感之后,我的问题的解决方案对我来说变得显而易见(我正在骑自行车)。 的“$now”部分
需要设置为当前日期。 不是“$time()-now”,而是“我关心的当前日期/我正在查看我的日志。
即:如果我正在查看 8/10/2008 的时间表摘要,那么根据 strtotime() 是“现在”;昨天是 8/09,明天是 8/11。 一旦我爬起来,“现在”就是 8/11,昨天是 8/10,明天是 8/12。
以下是代码示例:
单击“<<”和“>>”前进和后退相关日期
After a moment of inspiration, the solution to my question became apparent to me (I was riding my bike). The '$now' part of
needs to be set as the current date. Not "$time()-now", but "the current date I'm concerned with / I'm looking at my log for.
ie: if I'm looking at the timesheet summary for 8/10/2008, then that is "now" according to strtotime(); yesterday is 8/09 and tomorrow is 8/11. Once I creep up one day, "now" is 8/11, yesterday is 8/10, and tomorrow is 8/12.
Here's the code example:
Clicking on the "<<" and ">>" advances and retreats the day in question
对于此类事情来说,从以前的调用到同一脚本并不是一个好主意。
您想要做的始终是将两个值传递给脚本:日期和运动。 (下面的示例经过简化,因此您只传递日期,并且它总是会添加一天)
示例
http://www.site.com/addOneDay.php?date=1999-12-31
请注意,您应该检查以确保 isset($_GET[date] )之前,
如果您确实想从以前的调用中工作到同一脚本,您将必须使用会话来完成此操作,因此请指定是否是这种情况。
Working from previous calls to the same script isn't really a good idea for this type of thing.
What you want to do is always pass two values to your script, the date, and the movement. (the below example is simplified so that you only pass the date, and it will always add one day to it)
Example
http://www.site.com/addOneDay.php?date=1999-12-31
Please note that you should check to make sure that isset($_GET[date]) before as well
If you really want to work from previous calls to the same script, you're going to have to do it with sessions, so please specify if that is the case.