php - 星期四的 strtotime 错误?

标签 php date strtotime

首先,我要做的是获取特定日期后接下来 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/

相关文章:

php mysql - 将相同数据的双行插入到 mysql 数据库中

php - 为什么 PHP 的 "password_hash"不返回字符串?

php - 使用 PHP 将 Excel 日期数字转换为 Unix 时间戳时不匹配

database - 如何在 laravel 中将日期转换为整数

php - 获取给定月份名称之后和之前的月份

PHP strtotime() "first monday february"如果 2 月 1 日是星期一,则返回第二个星期一

PHP 简写标签 7.x

php - Facebook 用户名验证正则表达式模式

date - 使用DATETIME25.6格式的HIVE日期范围选择

java - 将字符串转换为日期并在 ArrayList Java 中进行比较