最近有效月份的时间戳 [英] Timestamp of nearest valid month

查看:194
本文介绍了最近有效月份的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Just a quickie.. How to derive the unixtime of nearest March or June?

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

If the current month is February of 2009, the script should give the unixtime of March 01, 2009.

如果当前月份是2009年4月,脚本应该提供2009年6月1日的unixtime。

If the current month is April of 2009, the script should give the unixtime of June 01, 2009.

如果当前月份是2009年10月,脚本应该提供2010年3月1日的unixtime。

If the current month is October of 2009, the script should give the unixtime of March 01, 2010.

谢谢任何帮助!

推荐答案

更新:对不起,我的坏下一个适用于几天,例如下个月,但不是明年3月,所以它比原来的一个班轮更复杂一些。

Update: Sorry, my bad. "next" works with days and in cases like "next month" but not "next March" so it's a little more convoluted than the original one liner.

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

strtotime() is really awesome for things like this:

$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);
  }
}

输出:

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

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

这篇关于最近有效月份的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆