Herhangi bir PHP kullanarak geçerli ayı ve önceki üç aylık almak için beni nasıl anlatacağım
Örneğin:
echo date("y:M:d");
çıkışı olacak: 09: Ekim: 20
Ama gerekir:
Ağustos
Eylül
Ekim
Çıktı olarak.
Şimdiden teşekkürler ...
Fero
Seyretmek dışarı için FUAH! Ayın 31 üzerinde yapıldığında, diğer cevaplar başarısız olur. Bunun yerine bu kullanın:
/*
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>";
Daha fazla bilgi için, benim cevap bakın here
Kullanmayı deneyin strtotime
a> PHP fonksiyonu ve tam metin çıktısı için 'F' ile inşa:
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