【问题标题】:Better way of creating a PHP future date创建 PHP 未来日期的更好方法
【发布时间】:2010-04-15 22:39:22
【问题描述】:

有没有更快的方法来创建日期,例如:

echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")+3,   date("Y")));

如果你能帮忙,谢谢。

【问题讨论】:

    标签: php date mktime


    【解决方案1】:

    strtotime()怎么样:

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

    【讨论】:

      【解决方案2】:

      您必须查看strtotime()。我想你的最终代码看起来像这样:

      $currentDate      = strtotime('today');//your date variable goes here
      $futureDate = date('Y-m-d', strtotime('+ 2 days', $currentDate));
      echo $futureDate;
      

      Live Demo

      如果您使用的是 PHP 版本 >= 5.2,我强烈建议您使用新的 DateTime 对象。例如如下:

      $futureDate = new DateTime("today");
      $futureDate->modify("+2 days");
      echo $futureDate->format("Y-m-d");
      

      Live Demo

      【讨论】:

        猜你喜欢
        • 2014-04-06
        • 2011-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-14
        • 1970-01-01
        相关资源
        最近更新 更多