你可以使用 strtotime() 函數(shù)將時(shí)間戳轉(zhuǎn)換為日期,并使用 date() 函數(shù)將日期格式化為年月。以下是一個(gè)示例代碼:
$startTimestamp = 1609459200; // 開始時(shí)間戳
$endTimestamp = 1612137599; // 結(jié)束時(shí)間戳
// 將開始時(shí)間戳轉(zhuǎn)換為年月
$startYearMonth = date('Y-m', $startTimestamp);
// 將結(jié)束時(shí)間戳轉(zhuǎn)換為年月
$endYearMonth = date('Y-m', $endTimestamp);
// 獲取范圍內(nèi)的年月
$yearMonths = [];
$currentYearMonth = $startYearMonth;
while ($currentYearMonth <= $endYearMonth) {
$yearMonths[] = $currentYearMonth;
$currentYearMonth = date('Y-m', strtotime($currentYearMonth . ' +1 month'));
}
// 輸出范圍內(nèi)的年月
foreach ($yearMonths as $yearMonth) {
echo $yearMonth . "\n";
}
在上面的示例中,我們先將開始時(shí)間戳和結(jié)束時(shí)間戳轉(zhuǎn)換為年月格式。然后,通過循環(huán)的方式逐個(gè)生成范圍內(nèi)的年月。最后,我們通過 foreach 循環(huán)來輸出這些年月。