只是一个快速.. 如何获取最近的三月或六月的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()支持哪些日期格式?
更多推荐
最近有效月份的时间戳
发布评论