【问题标题】:PHP add date to month end [duplicate]PHP将日期添加到月末[重复]
【发布时间】:2012-11-15 06:47:45
【问题描述】:

可能重复:
PHP last day of the month

我必须为我的申请设置一个到期日期。

第一个用户必须选择上市时间,例如它的 1 年,今天是 2012 年 11 月 25 日

然后我们必须将此 1 年添加到当前日期并自动将到期日期设置为其月末。即到期日将是30-Nov-2013

如果当前日期是 2012 年 10 月 4 日并选择 1 年持续时间,则到期日期是 2012 年 10 月 31 日。

我成功地在当前日期添加了一/二/任何年份,但是如何找出它的月底?

【问题讨论】:

标签: php


【解决方案1】:

你可以这样做:

echo date("Y-m-t", strtotime("+1 year"));

See the DOCs for strtotime

还有using t for the date function to get the last day of the month

【讨论】:

  • 这不会返回当月最后一天的返回日期
  • Ta,快点打败你吧 ;-)
  • @Alex 不,你没有。查看我的评论与您的​​帖子的时间戳:-P
  • 我发誓,上面写着你的 38 秒和我的一对。它在撒谎!
【解决方案2】:

以下内容应该适合您:

echo date("Y-m-t", strtotime("+1 year"));

【讨论】:

  • :-P 是的。很抱歉之前的错误。
【解决方案3】:

试试这个代码:

echo date(date('t',strtotime($date)) . '-M-Y', strtotime($date . ' +1 year'));

【讨论】:

    猜你喜欢
    • 2013-11-18
    • 2013-07-14
    • 2012-12-19
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    相关资源
    最近更新 更多