How can i get the current month and previous three months using PHP
Will any one tell me how to get the current month and previous three months using PHP
For example:
echo date("y:M:d");
output will be: 09:Oct:20
But i need:
August
September
October
as the Output.
Thanks in ad开发者_运维知识库vance...
Fero
for full textual representation of month you need to pass "F":
echo date("y:F:d");
for previous month you can use
echo date("y:F:d",strtotime("-1 Months"))
;
Watch out for the FUAH! The other answers will fail when performed on the 31st of the month. Use this instead:
/*
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
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')));
Try using the built in strtotime
function in PHP and using 'F' for full textual output:
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
If you want to be OOP about it, try this:
$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
You'll need to use date("F"); to get the full text representation of the date.
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
echo date('F', strtotime('-2 month')), '<br>',
date('F', strtotime('last month')), '<br>',
date('F');
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
精彩评论