php 如何使用PHP获得上一年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5896345/
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
How to get previous year using PHP
提问by Fero
How am I able to get the value of the previous year using PHP. Are there any predefined functions for it?
我如何能够使用 PHP 获得上一年的价值。是否有任何预定义的功能?
回答by diEcho
try
尝试
echo date("Y",strtotime("-1 year"));
回答by David Stutz
$year = date("Y");
$previousyear = $year -1;
回答by Dunhamzzz
There are many ways, you can either take away the amount of seconds in a year from time()
like so:
有很多方法,您可以time()
像这样减少一年中的秒数:
$prevYear = date('Y', time() - 60*60*24*365 );
$prevYear = date('Y', time() - 60*60*24*365 );
Or if you'd prefer, use the PHPs clever strtotime()
function:
或者,如果您愿意,也可以使用 PHP 的智能strtotime()
功能:
$prevYear = date('Y', strtotime('-1 year'));
$prevYear = date('Y', strtotime('-1 year'));
Or even like others have said, if it's from todays year just do date('Y') -1
或者甚至像其他人所说的,如果是从今天开始就做 date('Y') -1
回答by michal.jakubeczy
Shortest approach:
最短的方法:
$lastYear = (int)date("Y") - 1;
回答by jimy
function adddate($vardate,$added)
{
$data = explode("-", $vardate);
$date = new DateTime();
$date->setDate($data[0], $data[1], $data[2]);
$date->modify("".$added."");
$day= $date->format("Y-m-d");
return $day;
}
echo "Example : " . adddate("2010-08-01","-1 year");
回答by Midnightly Coder
If you want to display the entire date exactly 1 year ago, including the month and year:
如果要显示 1 年前的整个日期,包括月份和年份:
<?php echo date("M d Y", strtotime("-1 year")); ?>
回答by Dev Danidhariya
Try This
尝试这个
date('Y', strtotime('last year'));
回答by HappyCoder
This thread needs an update.
这个线程需要更新。
Nowadays you would use the PHP date-time object and do something like this:
如今,您将使用 PHP 日期时间对象并执行以下操作:
$lastYear = new DateTime();
$lastYear->sub(new DateInterval('P1Y'));
echo $lastYear->format('Y');
回答by Areej Qasrawi
you can give a value of the input tag as :
您可以将输入标签的值指定为:
<?php echo date("Y-m-d",strtotime("-1 year"));?>