最近有效月份的时间戳

编程入门 行业动态 更新时间:2024-10-24 20:14:49
本文介绍了最近有效月份的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

只是一个快速.. 如何获取最近的三月或六月的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()支持哪些日期格式?

更多推荐

最近有效月份的时间戳

本文发布于:2023-10-25 14:16:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527199.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!