如何使用 PHP 获取当前月份和前三个月
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1594277/
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 can i get the current month and previous three months using PHP
提问by Fero
Will any one tell me how to get the current month and previous three months using PHP
有没有人告诉我如何使用PHP获取当前月份和前三个月
For example:
例如:
echo date("y:M:d");
output will be: 09:Oct:20
输出将是: 09:Oct:20
But i need:
但是我需要:
August
八月
September
九月
October
十月
as the Output.
作为输出。
Thanks in advance...
提前致谢...
Fero
费罗
回答by Deniss Kozlovs
for full textual representation of month you need to pass "F":
对于月份的全文表示,您需要通过“F”:
echo date("y:F:d");
for previous month you can use
上个月你可以使用
echo date("y:F:d",strtotime("-1 Months"));
echo date("y:F:d",strtotime("-1 Months"));
回答by Yarin
Watch out for the FUAH! The other answers will fail when performed on the 31st of the month. Use this instead:
当心FUAH!在当月 31 日执行时,其他答案将失败。改用这个:
/*
Handles month/year increment calculations in a safe way,
avoiding the pitfall of 'fuzzy' month units.
Returns a DateTime object with incremented month values, and a date value == 1.
*/
function incrementDate($startDate, $monthIncrement = 0) {
$startingTimeStamp = $startDate->getTimestamp();
// Get the month value of the given date:
$monthString = date('Y-m', $startingTimeStamp);
// Create a date string corresponding to the 1st of the give month,
// making it safe for monthly calculations:
$safeDateString = "first day of $monthString";
// Increment date by given month increments:
$incrementedDateString = "$safeDateString $monthIncrement month";
$newTimeStamp = strtotime($incrementedDateString);
$newDate = DateTime::createFromFormat('U', $newTimeStamp);
return $newDate;
}
$currentDate = new DateTime();
$oneMonthAgo = incrementDate($currentDate, -1);
$twoMonthsAgo = incrementDate($currentDate, -2);
$threeMonthsAgo = incrementDate($currentDate, -3);
echo "THIS: ".$currentDate->format('F Y') . "<br>";
echo "1 AGO: ".$oneMonthAgo->format('F Y') . "<br>";
echo "2 AGO: ".$twoMonthsAgo->format('F Y') . "<br>";
echo "3 AGO: ".$threeMonthsAgo->format('F Y') . "<br>";
For more, see my answer here
有关更多信息,请在此处查看我的答案
回答by martin.malek
this month
这个月
date("y:M:d", mktime(0, 0, 0, date('m'), date('d'), date('Y')));
previous months
前几个月
date("y:M:d", mktime(0, 0, 0, date('m') - 1, date('d'), date('Y')));
date("y:M:d", mktime(0, 0, 0, date('m') - 2, date('d'), date('Y')));
回答by Nathan Kleyn
Try using the built in strtotimefunction in PHP and using 'F' for full textual output:
尝试使用strtotimePHP 中的内置函数并使用 'F' 进行全文输出:
echo date('y:F:d'); // first month
echo date('y:F:d', strtotime('-1 month')); // previous month
echo date('y:F:d', strtotime('-2 month')); // second previous month
echo date('y:F:d', strtotime('-3 month')); // third previous month
回答by dnagirl
If you want to be OOP about it, try this:
如果你想成为 OOP,试试这个:
$dp=new DatePeriod(date_create(),DateInterval::createFromDateString('last month'),2);
foreach($dp as $dt) echo $dt->format("y:M:d"),"\n"; //or "y F d"
outputs:
输出:
- 09:Oct:20
- 09:Sep:20
- 09:Aug:20
- 09:10:20
- 09:9:20
- 09:8:20
回答by Abi
DECLARE @STARTDATE INT
SET @STARTDATE = -12
WHILE @STARTDATE < 0
BEGIN
PRINT DATENAME(MONTH,DATEADD(MM,@STARTDATE,GETDATE()))+' '+ DATENAME(YEAR, DATEADD(MM,@STARTDATE ,GETDATE()))
SET @STARTDATE =@STARTDATE+1
END
回答by somacore
You'll need to use date("F"); to get the full text representation of the date.
你需要使用 date("F"); 获取日期的全文表示。
回答by vava
echo date('F', strtotime('-2 month')), '<br>',
date('F', strtotime('last month')), '<br>',
date('F');
回答by rubi
DECLARE @STARTDATE VARCHAR(MAX)
DECLARE @ENDDATE VARCHAR(MAX)
DECLARE @A INT
SET @A=-12
SET @STARTDATE= DATENAME(MONTH,GETDATE())+ DATENAME(YEAR, DATEADD(MONTH,0,GETDATE()))
WHILE @A < 0
BEGIN
SET @STARTDATE = DATENAME(MONTH,DATEADD(MONTH,@A,GETDATE()))+' '+ DATENAME(YEAR, DATEADD(MONTH,@A,GETDATE()))
SET @A=@A+1
PRINT @STARTDATE
END

