77

我有这个 PHP 代码:

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

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

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

我怎样才能做到这一点?

4

12 回答 12

169

尝试:

$end = date('Y-m-d', strtotime('+5 years'));
于 2013-08-16T09:02:33.470 回答
28

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

Procedural

    $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

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

另一个适合您情况的示例可能是: 基于此帖子

    <?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));
    ?>
于 2013-08-16T09:20:43.143 回答
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

您还可以减去替换 + 到 -

于 2014-09-29T08:38:23.543 回答
8
       $date = strtotime($row['timestamp']);
       $newdate = date('d-m-Y',strtotime("+1 year",$date));
于 2017-06-15T17:51:58.893 回答
5

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

于 2016-04-28T05:28:50.403 回答
2

使用

$dt = Carbon::now();
echo $dt->addYears(5); 
于 2013-08-16T09:02:14.247 回答
1

要将一年添加到今天的日期,请使用以下命令:

$oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day"));
于 2013-08-16T09:01:47.717 回答
1

试试下面的代码,希望对你有帮助

<?php

$current_date=strtotime(date('Y-m-d'));

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

?>

于 2021-07-13T10:52:41.493 回答
1

您可以为此目的使用 DateInterval;

$currentDate = new \DateTime(); //creates today timestamp
$currentDate->add(new \DateInterval('P5Y')); //this means 5 Years
and you can now format it;
$currentDate->format('Y-m-d');
于 2021-02-22T15:41:07.873 回答
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;
于 2013-08-16T09:10:06.550 回答
0

尝试这个:

$yearnow= date("Y");
$yearnext=$yearnow+1;
echo date("Y")."-".$yearnext;
于 2016-10-04T19:19:34.620 回答
0

Try this code and add next Days, Months and Years

// 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>';
}
于 2018-08-08T10:07:02.213 回答