使用php日期/时间类我试图获得某些特定日期

时间:2010-09-21 09:29:24

标签: php datetime

我正在尝试找到修改DateTime对象的正确字符串。我有一个定期的日历对象:

目前发现'下个月的这个日期'很容易:$ start_date-> modify('+ 1 month');

但是,我也希望找到“下个月的这一天”和“明年的这一天”。

无法找到相关的字符串。 干杯

编辑:

例如,如果我们将2010-09-21作为开始日期:

$start_date = new DateTime(20100921);

查找下个月的当前日期(数字表示)将是:

$start_date->modify('+1 month');

然而,找到下个月的当前日期(文字表示)会给我带来更多麻烦。

This date is the third tuesday of this month - next month the third tuesday is the 19th

1 个答案:

答案 0 :(得分:1)

你可以在“下个月的同一天”做这样的事情:

<?php

switch(floor(date("d") / 7)) {
  case 0:
    $which = "first";
    break;
  case 1:
    $which = "second";
    break;
  case 2:
    $which = "third";
    break;
  case 3:
    $which = "fourth";
    break;
  case 4:
    $which = "fifth";
    break;
}

echo date("c", strtotime(sprintf("%s %s of +1 month", $which, date("l"))));

我不知道如果同样适用于“明年的同一天”(fiftysecond thuesday of +1 year),但如果您知道年份的哪一天开始然后使用mktime(),您也可以计算它。