105

我试图从我指定的日期起一年的日期。

我的代码如下所示:

$futureDate=date('Y-m-d', strtotime('+one year', $startDate));

它返回错误的日期。任何想法为什么?

4

14 回答 14

236
$futureDate=date('Y-m-d', strtotime('+1 year'));

$futureDate 是一年后!

$futureDate=date('Y-m-d', strtotime('+1 year', strtotime($startDate)) );

$futureDate 距离 $startDate 一年!

于 2013-01-25T15:17:08.760 回答
106

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

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

对于其他示例,您必须使用时间戳值初始化 $StartingDate,例如:

$StartingDate = mktime();  // todays date as a timestamp

尝试这个

$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 365 day"));

或者

$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StaringDate)) . " + 1 year"));
于 2009-12-15T09:09:37.067 回答
11
//1 year from today's date
echo date('d-m-Y', strtotime('+1 year'));

//1 year from from specific date
echo date('22-09-Y', strtotime('+1 year'));

希望这个更简单的代码对将来的人有所帮助:)

于 2017-07-31T15:26:44.320 回答
9

尝试:$futureDate=date('Y-m-d',strtotime('+1 year',$startDate));

于 2009-12-15T03:52:59.520 回答
7

只是有同样的问题,但这是最简单的解决方案:

<?php (date('Y')+1).date('-m-d'); ?>
于 2013-03-29T09:18:27.107 回答
7
// Declare a variable for this year 
$this_year = date("Y");
// Add 1 to the variable
$next_year = $this_year + 1;
$year_after = $this_year + 2;

// Check your code
    echo "This year is ";
    echo $this_year;
    echo "<br />";
    echo "Next year is ";
    echo $next_year;
    echo "<br />";
    echo "The year after that is ";
    echo $year_after;
于 2016-01-06T23:59:02.307 回答
5

我更喜欢 OO 方法:

$date = new \DateTimeImmutable('today'); //'today' gives midnight, leave blank for current time.
$futureDate = $date->add(\DateInterval::createFromDateString('+1 Year'))

使用DateTimeImmutable否则您也会修改原始日期!更多关于 DateTimeImmutable: http: //php.net/manual/en/class.datetimeimmutable.php


如果您只想从今天开始,那么您可以随时执行以下操作:

new \DateTimeImmutable('-1 Month');
于 2016-12-14T17:06:01.067 回答
3

strtotime()正在返回bool(false),因为它无法解析字符串'+one year'(它不理解“one”)。 false然后被隐式转换为integertimestamp 0。在将 ' 的输出推入其他函数之前验证它strtotime()的输出是一个好主意。bool(false)

从文档:

返回值

成功返回时间戳,否则返回 FALSE。在 PHP 5.1.0 之前,此函数将在失败时返回 -1。

于 2009-12-15T04:00:43.087 回答
3

如果您使用的是 PHP 5.3,那是因为您需要设置默认时区:

date_default_timezone_set()
于 2009-12-15T03:55:09.750 回答
2

尝试这个

$nextyear  = date("M d,Y",mktime(0, 0, 0, date("m",strtotime($startDate)),   date("d",strtotime($startDate)),   date("Y",strtotime($startDate))+1));
于 2009-12-15T03:56:46.580 回答
1

还有一个更简单且不太复杂的解决方案:

$monthDay = date('m/d');
$year = date('Y')+1;
$oneYearFuture = "".$monthDay."/".$year."";
echo"The date one year in the future is: ".$oneYearFuture."";
于 2013-06-16T04:59:34.080 回答
1

我的解决方案是:date('Y-m-d', time()-60*60*24*365);

您可以通过定义使其更具“可读性”:

define('ONE_SECOND', 1);
define('ONE_MINUTE', 60 * ONE_SECOND);
define('ONE_HOUR',   60 * ONE_MINUTE);
define('ONE_DAY',    24 * ONE_HOUR);
define('ONE_YEAR',  365 * ONE_DAY);

date('Y-m-d', time()-ONE_YEAR);
于 2020-05-22T13:41:10.463 回答
0

就我而言(我想在当前日期增加 3 年),解决方案是:

$future_date = date('Y-m-d', strtotime("now + 3 years"));

致加登尼、特雷比和丹尼尔·利马:2 月 29 日会发生什么?有时二月只有 28 天 :)

于 2014-11-04T13:48:27.973 回答
0

您可以使用 strtotime() 来获取未来时间。

//strtotime('+1 day');
//strtotime('+1 week');
//strtotime('+1 month');

 $now = date('Y-m-d'); 
 $oneYearLaterFromNow = date('Y-m-d', strtotime('+1 year'));
 $oneYearLaterFromAnyDate = date('Y-m-d', strtotime('+1 year', strtotime($anyValidDateString)));
于 2021-10-28T11:05:58.950 回答