php $日期 + 1 年?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1905048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:15:11  来源:igfitidea点击:

$date + 1 year?

phpstrtotime

提问by Matt

I'm trying to get a date that is one year from the date I specify.

我试图获得一个自我指定的日期起一年的日期。

My code looks like this:

我的代码如下所示:

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

It's returning the wrong date. Any ideas why?

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

回答by Misho

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

$futureDate is one year from now!

$futureDate 是一年后!

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

$futureDate is one year from $startDate!

$futureDate 是 $startDate 的一年!

回答by Nidhin Baby

To add one year to todays date use the following:

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

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

For the other examples you must initialize $StartingDate with a timestamp value for example:

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

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

Try this

尝试这个

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

or

或者

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

回答by K Prime

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

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

回答by AnarchyOutlaw

// 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;

回答by Gardenee

just had the same problem, however this was the simplest solution:

刚刚遇到了同样的问题,但这是最简单的解决方案:

<?php (date('Y')+1).date('-m-d'); ?>

回答by Andrew Atkinson

I prefer the OO approach:

我更喜欢面向对象的方法:

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

Use DateTimeImmutableotherwise you will modify the original date too! more on DateTimeImmutable: http://php.net/manual/en/class.datetimeimmutable.php

使用DateTimeImmutable,否则你将改变原来的日期呢!更多关于 DateTimeImmutable:http: //php.net/manual/en/class.datetimeimmutable.php



If you just want from todays date then you can always do:

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

new \DateTimeImmutable('-1 Month');

回答by Developer

//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'));

hope this simpler bit of code helps someone in future :)

希望这段更简单的代码可以帮助将来的某个人:)

回答by Frank Farmer

strtotime()is returning bool(false), because it can't parse the string '+one year'(it doesn't understand "one"). falseis then being implicitly cast to the integertimestamp 0. It's a good idea to verify strtotime()'s output isn't bool(false)before you go shoving it in other functions.

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

From the docs:

从文档:

Return Values

Returns a timestamp on success, FALSE otherwise. Previous to PHP 5.1.0, this function would return -1 on failure.

返回值

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

回答by SeanJA

If you are using PHP 5.3, it is because you need to set the default time zone:

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

date_default_timezone_set()

回答by Treby

Try This

尝试这个

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