星期四的 strtotime 错误?

发布于 2024-12-11 12:52:04 字数 2673 浏览 0 评论 0原文

首先,我想做的是获取特定日期后接下来 7 天的日期。对于第一组,我希望我的结果是 2011 年 11 月 1 日到 2011 年 11 月 7 日。这是代码以及它给我的结果。这非常非常奇怪。

$date = "October 31, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "saturday");  
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "sunday");

它给了我:

2011-10-31    <---    I Want the Next Monday
2011-11-01
2011-11-02
1969-12-31    <---    ???????
2011-11-04
2011-11-05
2011-11-06

那么我将其更改为这样:

$date = "October 31, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "next monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "next tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "next wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "next thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "next friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "next saturday"); 
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "next sunday");

它给了我:

2011-11-07    <---  Wow, it works!
2011-11-01    <---  Wow, it works!
2011-11-02    <---  Wow, it works!
2011-11-03    <---  Wow, it works!
2011-11-04    <---  Wow, it works!
2011-11-05    <---  Wow, it works!
2011-11-06    <---  Wow, it works!

现在我尝试使用等于今天日期的日期:

$date = "October 22, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "next monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "next tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "next wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "next thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "next friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "next saturday"); 
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "next sunday");

我得到:

2011-10-24
2011-10-25
2011-10-26
2011-10-27
2011-10-28
2011-10-29
2011-10-23   Hooray, it still works!

我似乎已经弄清楚如何获得我想要的东西,但是事实是,在我输入这个问题之前,它似乎并没有正常工作。现在突然就这样了。在不改变任何东西的情况下,它似乎有时有效,有时无效。 1969 年的第一个结果很奇怪,我感觉我不知道发生了什么。

这看起来是完成我想做的事情的好方法吗?

如果有人知道输入日期并获取接下来 7 个日期的更好方法,你能帮我吗?或者跟我说说 1969 年到底发生了什么?一切似乎都工作正常,直到昨天,当我注意到周四的结果没有显示在我的页面上时,我追踪到了这一点。感谢您抽出时间。

First of all, what I am trying to do is get the dates of the next 7 days after a specific date. For this first set, I want my results to be 11/1/11 to 11/7/11. Here is the code, and the result that it gives me. This is very, very strange.

$date = "October 31, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "saturday");  
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "sunday");

And it gives me:

2011-10-31    <---    I Want the Next Monday
2011-11-01
2011-11-02
1969-12-31    <---    ???????
2011-11-04
2011-11-05
2011-11-06

So then I change it to this:

$date = "October 31, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "next monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "next tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "next wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "next thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "next friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "next saturday"); 
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "next sunday");

And it gives me:

2011-11-07    <---  Wow, it works!
2011-11-01    <---  Wow, it works!
2011-11-02    <---  Wow, it works!
2011-11-03    <---  Wow, it works!
2011-11-04    <---  Wow, it works!
2011-11-05    <---  Wow, it works!
2011-11-06    <---  Wow, it works!

Now I try it with the date equal to today's date:

$date = "October 22, 2011";
$nxtMon = strtotime(date("Y-m-d", strtotime($date)) . "next monday"); 
$nxtTue = strtotime(date("Y-m-d", strtotime($date)) . "next tuesday");
$nxtWed = strtotime(date("Y-m-d", strtotime($date)) . "next wednesday"); 
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . "next thursday");
$nxtFri = strtotime(date("Y-m-d", strtotime($date)) . "next friday"); 
$nxtSat = strtotime(date("Y-m-d", strtotime($date)) . "next saturday"); 
$nxtSun = strtotime(date("Y-m-d", strtotime($date)) . "next sunday");

And I Get:

2011-10-24
2011-10-25
2011-10-26
2011-10-27
2011-10-28
2011-10-29
2011-10-23   Hooray, it still works!

I seem to have figured out how to get exactly what I want, but the truth of the matter is that until I typed out this question it didn't seem to be working right. Now suddenly it does. It seems to work sometimes and not other times without changing anything. With the bizarreness of that 1969 in the first result, I'm feeling like I have no idea what is going on.

Does this look like a sound way to do what I am trying to do?

If anyone knows a better way to input a date and get the next 7 dates, could you help me out? Or throw me a bone as to what that 1969 is about? Everything seemed to be working fine until yesterday when I noticed Thursday results weren't showing up on my page and I traced it to this. Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

给妤﹃绝世温柔 2024-12-18 12:52:04

我认为您在星期几之前需要一个空格:

$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . " thursday");

我不知道为什么“星期四”与一周中的其他日子会得到不同的结果,但插入空格无论如何都是有意义的(“2011” -10-31 星期四”“2011-10-31 星期四”)。

I think you need a space before the day of the week:

$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . " thursday");

I don't know why you're getting different results for "thursday" vs. other days of the week, but inserting a space makes sense anyway ("2011-10-31 thursday" vs. "2011-10-31thursday").

幽梦紫曦~ 2024-12-18 12:52:04

呃...你不想这样做吗...?

$dateFrom = strtotime($date);
$nxtMon = strtotime('next monday', $dateFrom);
// ...
$nxtSun = strtotime('next sunday', $dateFrom);

根据您所描述的希望最终结果如上所示,在我看来这应该是您需要做的全部。看起来你对我来说有点过于复杂了,而且你可能没有阅读 strtotime() 的手册页完整 - 请注意第二个参数...

Erm... don't you want to be doing this...?

$dateFrom = strtotime($date);
$nxtMon = strtotime('next monday', $dateFrom);
// ...
$nxtSun = strtotime('next sunday', $dateFrom);

From what you describe as wanting the end result to be above, it seems to me like this should be all you need to do. Looks like you are somewhat over complicating it to me, plus you may not have read the manual page for strtotime() fully - note the second argument...

德意的啸 2024-12-18 12:52:04

您要求 strtotime 解释一个相当无意义的字符串,例如 2011-10-11next Monday。如您所见,这在某些情况下有效,但在其他情况下无效。我不会依赖它。这更加稳健:

strtotime('next monday', strtotime($date))

这会将 $date 转换为时间戳并请求相对于该时间戳的“下周一”。这应该会好很多。或者,做一些手动计算:

$date  = 'October 31, 2011';
$ts    = strtotime($date);
$day   = date('j', $ts);
$month = date('n', $ts);
$year  = date('Y', $ts);

for ($i = 0; $i < 7; $i++) {
    echo date('Y-m-d', mktime(0, 0, 0, $month, $day + $i, $year)) . PHP_EOL;
}

You are asking strtotime to interpret a rather nonsensical string like 2011-10-11next monday. As you see this works in some cases and doesn't in others. I wouldn't rely on it. This is more robust:

strtotime('next monday', strtotime($date))

This turns $date into a timestamp and requests the "next monday" relative to that timestamp. This should work a lot better. Alternatively, do a little manual calculation:

$date  = 'October 31, 2011';
$ts    = strtotime($date);
$day   = date('j', $ts);
$month = date('n', $ts);
$year  = date('Y', $ts);

for ($i = 0; $i < 7; $i++) {
    echo date('Y-m-d', mktime(0, 0, 0, $month, $day + $i, $year)) . PHP_EOL;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文