PHP日期加上5年到当前日期


71

我有这个PHP代码:

$end=date('Y-m-d');

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

$end=date('(Y + 5)-m-d');

我怎样才能做到这一点?



在a年的2月29日突然变得3月1日,这会有点古怪-尝试操纵日期时要注意这种奇怪之处
Mark Ba​​ker

Answers:


156

尝试:

$end = date('Y-m-d', strtotime('+5 years'));

3
嗨,我需要使用此示例在日期上加上1年,但是我已经有了变量:$invoicedate = $_POST['invdate']; $invdate = date ("Y-m-d", strtotime($invoicedate));那么我是否要增加第3行$due = date('Y-m-d', strtotime($invdate,'+1 year'));
阿德里安

25

根据这篇文章 修改日期
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

约会时间

    $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

您可以在strtotime()上抛出的内容非常令人惊讶,并且易于阅读。看一下这个例子,寻找下周的星期二。

程序

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

约会时间

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

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

另一个适合您的情况的示例可能是: 根据这篇文章

    <?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));
    ?>

13

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

 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

您还可以将+替换为-


7
       $date = strtotime($row['timestamp']);
       $newdate = date('d-m-Y',strtotime("+1 year",$date));

5

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


Carbon类及其方法属于特定的框架(Laravel),在提出的问题中,我们找不到任何框架名称,而是使用PHP日期函数来询问。我认为这就是为什么您没有竖起大拇指的原因:)
阿伦·辛格

实际上,碳并不取决于特定的框架。任何人都可以通过作曲家吸入碳并使用它。如果您不想检查核心PHP的功能,这是一种更轻松的解决方法。
Bijaya Prasad Kuikel

1

要为今天添加一年,请使用以下命令:

$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day"));

这是伟大的非常感谢你:)我正在接受后片刻

@字母看看我的回答-简单得多。
hsz


0

尝试这个 ,

$presentyear = '2013-08-16 12:00:00';

$nextyear  = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($presentyear )),   date("d",strtotime($presentyear )),   date("Y",strtotime($presentyear ))+5));

echo $nextyear;


0

尝试使用此代码并添加接下来的天,月和年

// current month: Aug 2018
$n = 2;
for ($i = 0; $i <= $n; $i++){
   $d = strtotime("$i days");
   $x = strtotime("$i month");
   $y = strtotime("$i year");
   echo "Dates : ".$dates = date('d M Y', "+$d days");
   echo "<br>";
   echo "Months : ".$months = date('M Y', "+$x months");
   echo '<br>';
   echo "Years : ".$years = date('Y', "+$y years");
   echo '<br>';
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.