php 兩個(gè)時(shí)間戳開始和結(jié)束,獲取范圍內(nèi)的年月

你可以使用 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)來輸出這些年月。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容