【问题标题】:why does <?php echo date("m/d/Y" strtotime('second wednesday June 2016')) ?> output 6/15/2016 instead of 6/8/2016?为什么 <?php echo date("m/d/Y" strtotime('second wednesday June 2016')) ?> 输出 6/15/2016 而不是 6/8/2016?
【发布时间】:2016-05-30 20:49:49
【问题描述】:

为什么

<?php echo date("m/d/Y" strtotime('second Wednesday June-2016')) ?>  

输出 2016 年 6 月 15 日而不是 2016 年 6 月 8 日?

这是我想出的解决方法 --- 不是很雄辩:

    <?php
    months = array( 1=>'Jan',2=>'Feb',3=>'Mar',4=>'Apr',5=>'May',6=>'Jun',7=>'Jul',8=>'Aug',9=>'Sep',10=>'Oct',11=>'Nov',12=>'Dec');
   $year = "2016";
    $month = "06";
    $smonth=$months[(int)$month];
    $tstr=$month."/01/".$year;
    $dow=date("D",strtotime($tstr));
    if ( $dow == "Wed" ) {
        $nstr="first";
    } else {
        $nstr="Second";
    }
    $edate= date("m/d/Y", strtotime($nstr.' Wednesday '.$smonth.'-'.$year));
    echo $edate."<br>";
    ?>

【问题讨论】:

  • 代替second 尝试使用2

标签: php date strtotime


【解决方案1】:

查看相关日期格式的文档: https://secure.php.net/manual/en/datetime.formats.relative.php

看起来他们都在月份和日期之间使用了“of”这个词。我用echo date("m/d/Y", strtotime('second Wednesday of June-2016')); 在我的本地服务器上尝试过它,它成功了。看起来省略“of”会导致未定义的行为。

【讨论】:

    【解决方案2】:

    你写了以下内容:

    <?php echo date("m/d/Y" strtotime('second Wednesday June-2016')) ?>
    

    它的两个问题是"m/d/Y" 后面应该有一个逗号,尽管我认为这个错字只是在 Stackoverflow 上。另一个问题是它应该说strtotime('second Wednesday of June 2016')

    漏掉of这个词可能会导致未知结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 2016-10-17
      • 1970-01-01
      相关资源
      最近更新 更多