PHP:获取第一个月前 6 个月日期的最简单方法?
如果今天是 2010 年 4 月 12 日 它应该返回 2009 年 10 月 1 日
我用谷歌搜索过的一些可能的解决方案似乎过于复杂,有什么建议吗?
So if today was April 12, 2010
it should return October 1, 2009
Some possible solutions I've googled seem overly complex, any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,也许是这样的;
编辑;
如果您想指定自定义日期使用;
Hm, maybe something like this;
EDIT;
if you would like to specify a custom date use;
有点黑客但有效:
A bit hackish but works:
使用
mktime
和date
:要使新日期相对于给定日期而不是今天,请使用第二个参数调用
date
格式化输出,只需再次使用
date
即可:use a combination of
mktime
anddate
:to make the new date relative to a given date and not today, call
date
with a second parameterto output it formatted, simply use
date
again:它在评论中进行了讨论,但接受的答案有一些不需要的
strtotime()
调用。可以简化为:另外,您可以像这样使用
DateTime()
,我认为这同样具有可读性:或者使用静态方法....
It was discussed in comments but the accepted answer has some unneeded
strtotime()
calls. Can be simplified to:Also, you can use
DateTime()
like this which I think is equally as readable:Or using static method....