php 日期减去 1 年?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1990321/
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:33:26  来源:igfitidea点击:

Date minus 1 year?

phpdate

提问by ajsie

I've got a date in this format:

我有一个这种格式的日期:

2009-01-01

How do I return the same date but 1 year earlier?

如何返回相同的日期但早于 1 年?

回答by CMS

You can use strtotime:

您可以使用strtotime

$date = strtotime('2010-01-01 -1 year');

The strtotimefunction returns a unix timestamp, to get a formatted string you can use date:

strtotime函数返回一个unix时间戳,以获取您可以使用的格式化字符串date

echo date('Y-m-d', $date); // echoes '2009-01-01'

回答by Alex

Use strtotime() function:

使用 strtotime() 函数:

  $time = strtotime("-1 year", time());
  $date = date("Y-m-d", $time);

回答by darrenwh

Using the DateTime object...

使用 DateTime 对象...

$time = new DateTime('2099-01-01');
$newtime = $time->modify('-1 year')->format('Y-m-d');

Or using now for today

或者现在使用今天

$time = new DateTime('now');
$newtime = $time->modify('-1 year')->format('Y-m-d');

回答by saadk

an easiest way which i used and worked well

我使用并运行良好的最简单方法

date('Y-m-d', strtotime('-1 year'));

this worked perfect.. hope this will help someone else too.. :)

这很完美..希望这也能帮助其他人..:)

回答by Nirmal

// set your date here
$mydate = "2009-01-01";

/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastyear = strtotime("-1 year", strtotime($mydate));

// format and display the computed date
echo date("Y-m-d", $lastyear);

回答by Meloman

On my website, to check if registering people is 18 years old, I simply used the following :

在我的网站上,为了检查注册人是否年满 18 岁,我简单地使用了以下内容:

$legalAge = date('Y-m-d', strtotime('-18 year'));

After, only compare the the two dates.

之后,只比较两个日期。

Hope it could help someone.

希望它可以帮助某人。

回答by Oliver Tappin

Although there are many acceptable answers in response to this question, I don't see any examples of the submethod using the \Datetimeobject: https://www.php.net/manual/en/datetime.sub.php

尽管对此问题有许多可接受的答案,但我没有看到任何sub使用该\Datetime对象的方法示例:https: //www.php.net/manual/en/datetime.sub.php

So, for reference, you can also use a \DateIntervalto modify a \Datetimeobject:

因此,作为参考,您还可以使用 a\DateInterval来修改\Datetime对象:

$date = new \DateTime('2009-01-01');
$date->sub(new \DateInterval('P1Y'));

echo $date->format('Y-m-d');

Which returns:

返回:

2008-01-01

For more information about \DateInterval, refer to the documentation: https://www.php.net/manual/en/class.dateinterval.php

有关 的更多信息\DateInterval,请参阅文档:https: //www.php.net/manual/en/class.dateinterval.php

回答by Oliver Tappin

You can use the following function to subtract 1 or any years from a date.

您可以使用以下函数从日期中减去 1 年或任何年份。

 function yearstodate($years) {

        $now = date("Y-m-d");
        $now = explode('-', $now);
        $year = $now[0];
        $month   = $now[1];
        $day  = $now[2];
        $converted_year = $year - $years;
        echo $now = $converted_year."-".$month."-".$day;

    }

$number_to_subtract = "1";
echo yearstodate($number_to_subtract);

And looking at above examples you can also use the following

查看上面的示例,您还可以使用以下内容

$user_age_min = "-"."1";
echo date('Y-m-d', strtotime($user_age_min.'year'));