PHP日期将当前日期添加1年

Let*_*ter 60 php

我有这个PHP代码:

$end=date('Y-m-d');
Run Code Online (Sandbox Code Playgroud)

我用它来获取当前日期,我需要将来5年的日期,例如:

$end=date('(Y + 5)-m-d');
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

hsz*_*hsz 143

试试:

$end = date('Y-m-d', strtotime('+5 years'));
Run Code Online (Sandbox Code Playgroud)

  • 嗨,我需要使用这个例子将1年添加到日期,但我已经有变量:`$ invoicedate = $ _POST ['invdate']; $ invdate = date("Ymd",strtotime($ invoicedate));`那么我会添加第3行`$ due = date('Ym-d',strtotime($ invdate,'+ 1 year')); `? (3认同)
  • @Sayuj3:使用几个月而不是几年。2.5年就是30个月。 (2认同)

Gad*_*oma 20

基于此帖子 修改日期
strtotime()非常强大,并且允许您使用它的相对表达式轻松修改/转换日期:

过程

    $dateString = '2011-05-01 09:22:34';
    $t = strtotime($dateString);
    $t2 = strtotime('-3 days', $t);
    echo date('r', $t2) . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100
Run Code Online (Sandbox Code Playgroud)

约会时间

    $dateString = '2011-05-01 09:22:34';
    $dt = new DateTime($dateString);
    $dt->modify('-3 days');
    echo $dt->format('r') . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100
Run Code Online (Sandbox Code Playgroud)

你可以在strtotime()抛出的东西是相当令人惊讶和非常人性化的.看看这个下周二寻找星期二的例子.

程序

    $t = strtotime("Tuesday next week");
    echo date('r', $t) . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100
Run Code Online (Sandbox Code Playgroud)

约会时间

    $dt = new DateTime("Tuesday next week");
    echo $dt->format('r') . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100
Run Code Online (Sandbox Code Playgroud)

请注意,上面的这些示例是相对于现在的时间返回的.strtotime()和DateTime构造函数采用的完整时间格式列表列在PHP支持的日期和时间格式页面上.

另一个例子,适合你的情况可能是: 基于这篇文章

    <?php
    //How to get the day 3 days from now:
    $today = date("j");
    $thisMonth = date("n");
    $thisYear = date("Y");
    echo date("F j Y", mktime(0,0,0, $thisMonth, $today+3, $thisYear)); 

    //1 week from now:
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
    echo date("F j Y", mktime(0,0,0, $thisMonth, $today+7, $thisYear));

    //4 months from now:
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
    echo date("F j Y", mktime(0,0,0, $thisMonth+4, $today, $thisYear)); 

    //3 years, 2 months and 35 days from now:
    list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
    echo date("F j Y", mktime(0,0,0, $thisMonth+2, $today+35, $thisYear+3));
    ?>
Run Code Online (Sandbox Code Playgroud)


RN *_*aha 12

使用此代码可将给定日期的年,月或日或小时或分钟或秒添加

 echo date("Y-m-d H:i:s", strtotime("+1 years", strtotime('2014-05-22 10:35:10'))); //2015-05-22 10:35:10
 echo date("Y-m-d H:i:s", strtotime("+1 months", strtotime('2014-05-22 10:35:10')));//2014-06-22 10:35:10
 echo date("Y-m-d H:i:s", strtotime("+1 days", strtotime('2014-05-22 10:35:10')));//2014-05-23 10:35:10
 echo date("Y-m-d H:i:s", strtotime("+1 hours", strtotime('2014-05-22 10:35:10')));//2014-05-22 11:35:10
 echo date("Y-m-d H:i:s", strtotime("+1 minutes", strtotime('2014-05-22 10:35:10')));//2014-05-22 10:36:10
 echo date("Y-m-d H:i:s", strtotime("+1 seconds", strtotime('2014-05-22 10:35:10')));//2014-05-22 10:35:11
Run Code Online (Sandbox Code Playgroud)

你也可以减去替换+到 -


Bij*_*kel 5

使用碳非常容易。 $date = "2016-02-16"; // Or Your date $newDate = Carbon::createFromFormat('Y-m-d', $date)->addYear(1);

  • 实际上碳并不依赖于特定的框架。任何人都可以通过 Composer 拉入 Carbon 并使用它。如果不想检查核心 PHP 功能,这是一种更简单的处理问题的方法。 (2认同)

小智 5

       $date = strtotime($row['timestamp']);
       $newdate = date('d-m-Y',strtotime("+1 year",$date));
Run Code Online (Sandbox Code Playgroud)