strtotime("下一次行军")

Nir*_*mal 1 php time

在这里经历一个奇怪的问题.

我想要获得即将到来的三月的日期,但它似乎不起作用.

$nextMarch = strtotime("next march");
Run Code Online (Sandbox Code Playgroud)

这个小小的代码有什么问题?

这是与此问题相关的问题:
最近有效月份的时间戳

Pas*_*TIN 9

我想你必须提供strtotime一些其他(和/或其他)信息.

例如,你想要进行游行的那一天的数量:

$nextMarch = strtotime("1 march");
var_dump($nextMarch, date('Y-m-d H:i:s', $nextMarch));
Run Code Online (Sandbox Code Playgroud)

但是今年你将获得游行:

int 1235862000
string '2009-03-01 00:00:00' (length=19)
Run Code Online (Sandbox Code Playgroud)

所以,要进行"下一次"游行,即明年:

$nextMarch = strtotime("1 march +1 year");
var_dump($nextMarch, date('Y-m-d H:i:s', $nextMarch));
Run Code Online (Sandbox Code Playgroud)

你得到:

int 1267398000
string '2010-03-01 00:00:00' (length=19)
Run Code Online (Sandbox Code Playgroud)

所以,也许你必须自己做一些计算,知道你是想要今年还是下一年.这样的事情可能会成功:

$nextMarch = strtotime("1 march");
if ($nextMarch < time()) {
  $nextMarch = strtotime("1 march +1 year");
}
var_dump($nextMarch, date('Y-m-d H:i:s', $nextMarch));
Run Code Online (Sandbox Code Playgroud)

(我真的不喜欢这个想法,但似乎工作 - 即使一个更简单的解决方案肯定会很好......)