0

我正在尝试使用该strtotime函数来返回指定的以下日期。例如:

strtotime('thursday'); //Need to get NEXT Thurs, not today (assuming today is Th)

在这种情况下使用next thursday将起作用,但不适用于绝对日期,例如:

strtotime('next aug 19'); //Should return 08-19-2011, but instead returns 12-31-1969

我认为这可能需要一些正则表达式,但由于将输入的格式如此多变,我宁愿找到一个更简单的解决方案(如果存在)。

谢谢!

4

4 回答 4

1

我没有办法知道。但是您可以询问当前年份,然后在必要时添加 1 年:

$d = new DateTime("Aug 19");
if (new DateTime() >= $d) {
    $d->add(new DateInterval("P1Y"));
}
echo $d->format(DateTime::W3C);
于 2010-08-19T16:43:07.230 回答
0

这就是我想出的。归功于@Artefacto帮助提出解决方案。

$interval = 'thursday';
$oldtime = strtotime('now');
$newtime = strtotime($interval);
$i = 0;
while ($oldtime >= $newtime) {
    $newtime = strtotime($interval, strtotime("+" . $i*$i . " hours"));
    $i++;
}

echo date("Y-m-d h:i:s", $newtime);
于 2010-08-19T17:56:59.350 回答
0

你能不使用 strtotime 方法的第二个参数,它是一个日期值,而不是今天为你提供计算的基础吗?因此,您可以将其设置为 8 月 19 日,然后计算 +1 年。

于 2010-08-19T16:37:01.087 回答
0

提出错误的问题将导致错误的答案。“下一个 8 月 19 日”可能是明年,下一个千年,...

尝试

strtotime('+1 year aug 19'); //or strtotime('+1 week aug 19');

快乐编码

于 2010-08-19T16:40:12.890 回答