【问题标题】:Php date and strtotime [closed]PHP日期和strtotime [关闭]
【发布时间】:2014-12-17 14:33:25
【问题描述】:

当我尝试将 2 个月添加到当前日期时,我遇到了 strtotime 问题

例子:

'2014-12-16 13' => date('Y-m-d H');

$compare_date = date('Y-m-d H:i',strtotime('2014-12-16 13'.'+2 month'));

【问题讨论】:

标签: php date datetime strtotime


【解决方案1】:

我在phpFiddle中测试过

如果您指定分钟,它会起作用(基于此代码)

$compare_date = date('Y-m-d H:i',strtotime('2014-12-16 13'.'+2 month'));

你说 H:i

所以如果你使用:

$compare_date = date('Y-m-d H:i',strtotime('2014-12-16 13:00'.'+2 month'));//Note the :00

它对我有用。

编辑1:

没有 H:i

$compare_date = date('Y-m-d',strtotime('2014-12-16'.'+2 month'));

EDIT2:

o-W

这对我有点用:

$compare_date = date('o-W',strtotime('2014-12-16'.'+2 month'));

EDIT3:

正如评论所说,去掉点

$compare_date = date('o-W',strtotime('2014-12-16 +2 month'));

【讨论】:

  • 但是,如果没有 H:i,我们怎么能做到呢?
  • 如果我们有 date('o-W') 我们如何将它与 strtotime 结合起来?
  • @YraYra without h:i done
  • $compare_date = date('Y-m-d H:i',strtotime('2014-12-16 13:00 +2 月'));摆脱。
  • 不,不,没有H:我很容易
【解决方案2】:

你用错了strtotime。使用相对时间(+2 个月)时,您应该提供时间戳作为第二个参数(当前时间戳是默认值)。

$compare_date = date('Y-m-d H:i',strtotime('+2 month', strtotime('2014-12-16 13:00')));

【讨论】:

    【解决方案3】:

    你试过了吗

    $compare_date = date('Y-m-d H',strtotime('2014-12-16 13'.'+2 month'));
    

    代替:

    $compare_date = date('Y-m-d H:i',strtotime('2014-12-16 13'.'+2 month'));
    

    【讨论】:

    • 我们还有其他方法吗?例如
    • 如果我有日期('o-W')
    【解决方案4】:

    注意使用 +month 如果下个月的天数较少,请注意以下行为

    echo date( "Y-m-d", strtotime( "2009-01-31 +1 month" ) ); // PHP:  2009-03-03
    echo date( "Y-m-d", strtotime( "2009-01-31 +2 month" ) ); // PHP:  2009-03-31
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多