最近有效月份的时间戳

Nir*_*mal 0 php time date

只是一个快速..如何得出最近的三月或六月的unixtime?

如果当前月份是2009年2月,则脚本应该给出2009年3月1日的unixtime.

如果当前月份是2009年4月,则脚本应该给出2009年6月1日的unixtime.

如果当前月份是2009年10月,则脚本应该给出2010年3月1日的unixtime.

感谢您的任何帮助!

cle*_*tus 5

更新:对不起,我的不好."下一个"适用于日子和"下个月"但不是"明年三月"的情况,所以它比原来的一个班轮更复杂.

strtotime() 对于这样的事情真的很棒:

$tests = array(
  strtotime('2 february'),
  strtotime('4 april'),
  strtotime('9 november'),
);

foreach ($tests as $test) {
  echo date('r', $test) . ' => ' . date('r', nextmj($test)) . "\n";
}

function nextmj($time = time()) {
  $march = strtotime('1 march', $time);
  $june = strtotime('1 june', $time);
  if ($march >= $time) {
    return $march;
  } else if ($june >= $time) {
    return $june;
  } else {
    return strtotime('+1 year', $march);
  }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Mon, 02 Feb 2009 00:00:00 +0000 => Sun, 01 Mar 2009 00:00:00 +0000
Sat, 04 Apr 2009 00:00:00 +0000 => Mon, 01 Jun 2009 00:00:00 +0000
Mon, 09 Nov 2009 00:00:00 +0000 => Mon, 01 Mar 2010 00:00:00 +0000
Run Code Online (Sandbox Code Playgroud)

另请参阅PHP函数strtotime()支持哪些日期格式?