【问题标题】:How to increase date by one month from fetched date from the database in php [duplicate]如何将日期从 php 中的数据库中获取的日期增加一个月 [重复]
【发布时间】:2018-12-03 10:17:48
【问题描述】:

我想将日期增加一个月,但是, 当前日期有效,但我需要将日期增加一个月(动态)

我从下面的回复中尝试了这个

 $regdate=$row2['created_date'];
 $onemonth = date($regdate,  strtotime("+1 month")); 

如何将 $regdate 变量添加到日期函数...?

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    试试这个

    $regdate=$row2['created_date'];
    $date = new DateTime($regdate);
    $interval = new DateInterval('P1M');
    
    $date->add($interval);
    $onemonth = $date->format('Y-m-d');
    

    【讨论】:

    • 感谢您的重播,但我想定义变量而不是 2000-12-31 任何建议 ..?
    • @Darshan 你是什么意思'我想定义变量而不是 2000-12-31'?我更改了代码 sn -p BTW
    • 是的,我试过了,但它显示为原样(未递增)
    • DateTimeImmutable 应该可以工作。可能是我做错了什么。它适用于 DateTime() 类。
    【解决方案2】:

    在我看来,如果您在 php 语句之前从数据库中编辑数据,我认为这将是完美的:

    MYSQL 级别:

     SELECT DATE_ADD( yourDate, INTERVAL 1 month ) from your table .
    

    【讨论】:

      【解决方案3】:

      试试这个

      $regdate = strtotime($row2['created_date']);
      echo date('Y-m-d',strtotime("+1 month",$regdate));
      

      【讨论】:

      • 我之前尝试过,但它对我不起作用 $regdate=2018-11-28 并且我在运行该代码后得到的是 =1970-02-01
      • @darshan 请检查一下,我做了一些更改。
      • 这是一个简单的方法谢谢@prashant
      【解决方案4】:

      尝试以下方法:

      在给定日期增加了 1 个月

        $date = $row2['created_date'];
        $date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " +1 month") );
        echo $date;
      

      您可以按如下方式添加所需的时间段:

      Add day
      $date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " +1 day") );
      
      Multiple days
      $date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " +15 days") );
      
      Add week
      $date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " +1 week") );
      
      Add month
      $date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " +1 month") );
      

      【讨论】:

      • 它的工作,这就是我要找的谢谢@manoj
      • @Darshan.. 很高兴为您提供帮助,您可以通过点赞将其标记为已接受的答案:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 2011-02-21
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多