Php 使用strotime获得一个月内的第一个工作日

Php 使用strotime获得一个月内的第一个工作日,php,datetime,strtotime,Php,Datetime,Strtotime,我试图用strotime计算出一个月的第一个星期三,但是只要第一个星期三正好在1号,“first-triday”参数就失败了 有关此问题的更一般说明,请参阅以下代码和结果: $mon = strtotime("December 2010 first monday"); $tue = strtotime("December 2010 first tuesday"); $wed = strtotime("December 2010 first wednesday"); $thu = strtotim

我试图用strotime计算出一个月的第一个星期三,但是只要第一个星期三正好在1号,“first-triday”参数就失败了

有关此问题的更一般说明,请参阅以下代码和结果:

$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>";
注意到什么了吗?一周中的一天不应该与一个月的第一天重合吗?但它从来没有这样做,相反,第二个实例的一天,在8日,总是返回


有人对此有什么解释吗?

我没有任何解释,因为我也感到眼花缭乱,但我设法通过省略“第一个”找到了正确的日期,如下所示:

$ 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

只是你的格式不正确。您需要包括的:

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"));
印刷品:

12/06/10
12/07/10
12/01/10
12/02/10
12/03/10
12/04/10
12/05/10
12/15/10
可接受的解决方案可行,但如果您想找到第二个工作日,则会遇到问题:

echo strftime("%m/%d/%y", strtotime("December 2010 second Wednesday"));
印刷品:

12/06/10
12/07/10
12/01/10
12/02/10
12/03/10
12/04/10
12/05/10
12/15/10
文件中给出了一些示例:

上述文档中的第二块注释解释了的
如何影响日期计算过程:

还要注意的是,“of”在“序数”中 空格dayname空格“of”和“last” 空格dayname空格“of”的作用 一些特别的东西

  • 它将月份的日期设置为1
  • “的序号dayname”不会提前到另一天。(例如: “2008年7月第一个星期二”是指 “2008-07-01”)
  • “序号日名”确实会提前到另一天。(例如:“第一 2008年7月28日星期二”指“2008-07-08”, 另请参见上面列表中的第4点)
  • “'last'dayname'of'”取当前月份的最后一天名。 (示例:“2008年7月最后一个星期三” 指“2008-07-30”)
  • “'last'dayname”从当前日期中获取最后一个dayname。 (例如:“上周三2008年7月”是指 《2008-06-25》;《2008年7月》首套 当前日期为“2008-07-01”,以及 然后“最后一个星期三”移到上一个星期三 星期三,即“2008-06-25”)

  • 在提供答案的基础上,需要注意PHP5.2和PHP5.3之间的相对日期格式的差异

    测试:

    $date1 = strtotime("first wednesday of 2010-12");
    $date2 = strtotime("first wednesday 2010-12");
    $date3 = strtotime("wednesday 2010-12");
    
    1969-12-31
    2010-12-08
    2010-12-01
    
    2010-12-01
    2010-12-08
    2010-12-01
    
    5.2结果:

    $date1 = strtotime("first wednesday of 2010-12");
    $date2 = strtotime("first wednesday 2010-12");
    $date3 = strtotime("wednesday 2010-12");
    
    1969-12-31
    2010-12-08
    2010-12-01
    
    2010-12-01
    2010-12-08
    2010-12-01
    
    5.3结果:

    $date1 = strtotime("first wednesday of 2010-12");
    $date2 = strtotime("first wednesday 2010-12");
    $date3 = strtotime("wednesday 2010-12");
    
    1969-12-31
    2010-12-08
    2010-12-01
    
    2010-12-01
    2010-12-08
    2010-12-01
    

    因此,只有第三种方法在PHP5版本中返回正确的结果。

    我在PHP手册中找到了这个注释

    “在5.2.7之前的PHP 5中,如果请求一个月内某个给定工作日的给定事件,而该工作日是该月的第一天,则会错误地将返回的时间戳增加一周。这在5.2.7及更高版本中已得到纠正。”

    PHP 5.5.11

    $firstWeekdayOfMonth=新日期时间(“2016年6月0日第一个工作日”)


    我不知道为什么,但是考虑到第一天,零会迫使strotime解析器在月初启动。

    我相信这是您放置值的顺序。 当你写“2010年12月”时,它已经是2010年1月12日了。在加入“第一
    {day}”,功能适用于2010年1月12日之后的第一天。我认为如果您先声明“first{day}”值,然后声明“december 2010”,它应该可以正常工作。

    如果您真的很好奇,您可以查看php源代码,至于我,让我们继续:DIt似乎不完全工作。我将于6月12日使用此解决方案,并于2014年12月15日获得
    12
    。也许它与当前月份的星期有关?'of'格式仅在PHP>=5.3上有效,在5.2上失败。测试结果见我的答案。@Yarin:很好。>=5.3中的日期处理似乎比早期版本可靠得多。真遗憾,有这么多潜在的陷阱。