首先,我要做的是获取特定日期后接下来 7 天的日期。对于第一组,我希望我的结果是 11/1/11 到 11/7/11。这是代码,以及它给我的结果。这非常非常奇怪。
$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 是关于什么的?一切似乎都运行良好,直到昨天我注意到星期四的结果没有显示在我的页面上,我才追踪到这一点。感谢您的宝贵时间。
最佳答案
我认为您需要在星期几之前留出空间:
$nxtThu = strtotime(date("Y-m-d", strtotime($date)) . " thursday");
我不知道为什么“星期四”与一周中的其他日子会得到不同的结果,但无论如何插入一个空格都是有意义的(“2011-10-31 星期四”
与 “2011-10-31thursday”
)。
关于php - 星期四的 strtotime 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7863208/