Yar*_*rin 11 php datetime strtotime
我试图弄清楚某个月的第一个星期三使用strtotime,但是"第一个星期三"的论点在第一个星期三恰好落在1号时就失败了.
有关此问题的更一般说明,请参阅以下代码和结果:
$mon = strtotime("December 2010 first monday");
$tue = strtotime("December 2010 first tuesday");
$wed = strtotime("December 2010 first wednesday");
$thu = strtotime("December 2010 first thursday");
$fri = strtotime("December 2010 first friday");
$sat = strtotime("December 2010 first saturday");
$sun = strtotime("December 2010 first sunday");
echo strftime("%m/%d/%y", $mon) . "<br>";
echo strftime("%m/%d/%y", $tue) . "<br>";
echo strftime("%m/%d/%y", $wed) . "<br>";
echo strftime("%m/%d/%y", $thu) . "<br>";
echo strftime("%m/%d/%y", $fri) . "<br>";
echo strftime("%m/%d/%y", $sat) . "<br>";
echo strftime("%m/%d/%y", $sun) . "<br>";
Run Code Online (Sandbox Code Playgroud)
结果是:
12/06/10
12/07/10
12/08/10
12/02/10
12/03/10
12/04/10
12/05/10
Run Code Online (Sandbox Code Playgroud)
注意什么?一周的哪一天不应该与本月的第一天一致?但它永远不会,而是在8日的第二天,总是返回.
有人对此有解释吗?
Yar*_*rin 10
建立关提供的答案,一个需要注意的PHP 5.2和PHP 5.3的相对的日期格式的差异.
测试:
$date1 = strtotime("first wednesday of 2010-12");
$date2 = strtotime("first wednesday 2010-12");
$date3 = strtotime("wednesday 2010-12");
Run Code Online (Sandbox Code Playgroud)
5.2结果:
1969-12-31
2010-12-08
2010-12-01
Run Code Online (Sandbox Code Playgroud)
5.3结果:
2010-12-01
2010-12-08
2010-12-01
Run Code Online (Sandbox Code Playgroud)
因此,只有第三种方法在PHP 5版本中返回正确的结果.
我没有任何解释,因为我也很眩目,但我设法通过省略"第一"找到了如何得到正确的日期,如下所示:
$ php -r 'echo date("m/d/y", strtotime("December 2010 Wednesday"));'
12/01/10
$ php -r 'echo date("m/d/y", strtotime("December 2010 Thursday"));'
12/02/10
$ php -r 'echo date("m/d/y", strtotime("December 2010 Friday"));'
12/03/10
$ php -r 'echo date("m/d/y", strtotime("December 2010 Saturday"));'
12/04/10
$ php -r 'echo date("m/d/y", strtotime("December 2010 Sunday"));'
12/05/10
$ php -r 'echo date("m/d/y", strtotime("December 2010 Monday"));'
12/06/10
$ php -r 'echo date("m/d/y", strtotime("December 2010 Tuesday"));'
12/07/10
Run Code Online (Sandbox Code Playgroud)
这只是你的格式不正确.你需要包括of:
echo strftime("%m/%d/%y", strtotime("first Monday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Tuesday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Wednesday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Thursday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Friday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Saturday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Sunday of December 2010"));
Run Code Online (Sandbox Code Playgroud)
打印:
12/06/10
12/07/10
12/01/10
12/02/10
12/03/10
12/04/10
12/05/10
Run Code Online (Sandbox Code Playgroud)
已接受的解决方案有效,但如果您想找到第二个工作日,则会遇到问题:
echo strftime("%m/%d/%y", strtotime("December 2010 second Wednesday"));
Run Code Online (Sandbox Code Playgroud)
打印:
12/15/10
Run Code Online (Sandbox Code Playgroud)
文档中给出了一些示例:
http://www.php.net/manual/en/datetime.formats.relative.php
上述文档中的第二个注释块说明了如何of影响日期计算过程:
还要注意''的'ordinal space dayname space'中的"of"和''''last'space spacename space'中的"of"做了一些特别的事情.
- 它将日期设置为1.
- "ordinal dayname"的''不会提前到另一天.(例如:"2008年7月的第一个星期二"是指"2008-07-01").
- "序数日"确实推进到另一天.(例如:"2008年7月1日星期二"表示"2008-07-08",另见上面列表中的第4点).
- "''的''last'dayname'取当月的最后一个日期名称.(例如:"2008年7月的最后一次婚礼"是指"2008-07-30")
- "'last'dayname"取自当天的最后一天.(例如:"2008年7月上旬"是指"2008-06-25";"2008年7月"首先将当前日期设置为"2008-07-01",然后"上次结婚"移至上一个周三,即"2008年7月" -06-25" ).
| 归档时间: |
|
| 查看次数: |
11536 次 |
| 最近记录: |