使用 strtotime() 推进相对日期

发布于 2024-07-04 04:05:09 字数 492 浏览 8 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉南桥 2024-07-11 04:05:10

凯文,你有一个坚实的绝对基础(即日期/时间),而不是相对时间段。 然后您可以转换为相对时间段。 因此,例如,默认情况下,如果您显示日历,则将从今天开始工作。

int strtotime  ( string $time  [, int $now  ] )

您可以在 strtotime 的函数定义中看到,第二个参数是 now,即您可以更改它的相对日期。

通过快速循环可能会更容易显示

这将使用“昨天”作为第一个参数循环显示过去 10 天。 然后我们使用日期将其打印出来。

$time = time();

for ($i = 0; $i < 10; $i++) {
    $time = strtotime("yesterday", $time);
    print date("r", $time) . "\n";
}

因此,请通过 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.

int strtotime  ( string $time  [, int $now  ] )

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.

$time = time();

for ($i = 0; $i < 10; $i++) {
    $time = strtotime("yesterday", $time);
    print date("r", $time) . "\n";
}

So pass the time/date in via the URI so you can save the relative date.

诠释孤独 2024-07-11 04:05:10

经过片刻的灵感之后,我的问题的解决方案对我来说变得显而易见(我正在骑自行车)。 的“$now”部分

strtottime( string $time {,int $now ]) 

需要设置为当前日期。 不是“$time()-now”,而是“我关心的当前日期/我正在查看我的日志。

即:如果我正在查看 8/10/2008 的时间表摘要,那么根据 strtotime() 是“现在”;昨天是 8/09,明天是 8/11。 一旦我爬起来,“现在”就是 8/11,昨天是 8/10,明天是 8/12。

以下是代码示例:

<?php

//catch variable
$givendate=$_GET['given'];

//convert given date to unix timestamp
$date=strtotime($givendate);
echo "Date Set As...: ".date('m/d/Y',$date)."<br />";

//use given date to show day before
$yesterday=strtotime('-1 day',$date);
echo "Day Before: ".date('m/d/Y',$yesterday)."<br />";

//same for next day
$tomorrow=strtotime('+1 day',$date);
echo "Next Day: ".date('m/d/Y',$tomorrow)."<br />";
$lastmonday=strtotime('last monday, 1 week ago',$date);
echo "Last Moday: ".date('D m/d/Y',$lastmonday)."<br />";

//form
echo "<form method=\"get\" action=\"{$_SERVER['PHP_SELF']}\">";

//link to subtract a day
echo "<a href=\"newtimetravel.php?given=".date('m/d/Y',$yesterday)."\"><< </a>";

//show current day
echo "<input type=\"text\" name=\"given\" value=\"$givendate\">";

//link to add a day
echo "<a href=\"newtimetravel.php?given=".date('m/d/Y',$tomorrow)."\"> >></a><br />";

//submit manually entered day
echo "<input type=\"submit\" name=\"changetime\" value=\"Set Current Date\">";

//close form
echo "<form><br />";
?>

单击“<<”和“>>”前进和后退相关日期

After a moment of inspiration, the solution to my question became apparent to me (I was riding my bike). The '$now' part of

strtottime( string $time {,int $now ]) 

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:

<?php

//catch variable
$givendate=$_GET['given'];

//convert given date to unix timestamp
$date=strtotime($givendate);
echo "Date Set As...: ".date('m/d/Y',$date)."<br />";

//use given date to show day before
$yesterday=strtotime('-1 day',$date);
echo "Day Before: ".date('m/d/Y',$yesterday)."<br />";

//same for next day
$tomorrow=strtotime('+1 day',$date);
echo "Next Day: ".date('m/d/Y',$tomorrow)."<br />";
$lastmonday=strtotime('last monday, 1 week ago',$date);
echo "Last Moday: ".date('D m/d/Y',$lastmonday)."<br />";

//form
echo "<form method=\"get\" action=\"{$_SERVER['PHP_SELF']}\">";

//link to subtract a day
echo "<a href=\"newtimetravel.php?given=".date('m/d/Y',$yesterday)."\"><< </a>";

//show current day
echo "<input type=\"text\" name=\"given\" value=\"$givendate\">";

//link to add a day
echo "<a href=\"newtimetravel.php?given=".date('m/d/Y',$tomorrow)."\"> >></a><br />";

//submit manually entered day
echo "<input type=\"submit\" name=\"changetime\" value=\"Set Current Date\">";

//close form
echo "<form><br />";
?>

Clicking on the "<<" and ">>" advances and retreats the day in question

寂寞陪衬 2024-07-11 04:05:09

对于此类事情来说,从以前的调用到同一脚本并不是一个好主意。

您想要做的始终是将两个值传递给脚本:日期和运动。 (下面的示例经过简化,因此您只传递日期,并且它总是会添加一天)

示例

http://www.site.com/addOneDay.php?date=1999-12-31

<?php
   echo Date("Y-m-d",(strtoTime($_GET[date])+86400));
?>

请注意,您应该检查以确保 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

<?php
   echo Date("Y-m-d",(strtoTime($_GET[date])+86400));
?>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文