【问题标题】:PHP adding months and days to the current time() and ovoid weekend daysPHP将月份和日期添加到当前时间()和卵形周末
【发布时间】:2014-08-13 12:25:16
【问题描述】:

我需要在 PHP 中的当前 time() 中添加 N 个月和 M 天。我目前的解决方案很简单,但我想知道它是否更简单:)?

$my_time = strtotime("+6 days", strtotime("+1 month", time()));

我也想知道在目标日期是否有机会避开周末(周六和周日)?

【问题讨论】:

  • strtotime("+6 天 1 个月", time());应该这样做

标签: php unix-timestamp strtotime


【解决方案1】:

是的,您可以将其简化为:

$my_time = strtotime("+6 days 1 month", time());

或者:

$my_time = strtotime("+6 days 1 month");

this

【讨论】:

  • 啊,我怎么能错过这个。我希望这样的事情会奏效,而且确实如此。 Gr8,谢谢。
【解决方案2】:

我会使用 DateTime 类,因为它比 strtotime 快得多。

$date = new DateTime();
$date->add(new DateInterval('P1M6D'));
echo $date->format('Y-m-d');

可以在 here 找到 DateInterval 的详细合成器信息。

【讨论】:

    【解决方案3】:

    试试下面的方法 使用 strtotime() 函数添加天数月数

    每日添加 date('Y-m-d', strtotime("+3 天")); 添加月份

    date('Y-m-d', strtotime("+3 month"));
    

    添加年份

    date('Y-m-d', strtotime("+3 year"));
    

    添加日月年

    date('Y-m-d', strtotime("+1 days 1 month 1 year "));
    

    减日月年

    date('Y-m-d', strtotime("-1 days 1 month 1 year "));
    

    只需使用 (+-) 符号

    【讨论】:

      猜你喜欢
      • 2018-04-28
      • 1970-01-01
      • 2020-02-26
      • 2012-11-15
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多