美文网首页
php 两个时间戳开始和结束,获取范围内的年月

php 两个时间戳开始和结束,获取范围内的年月

作者: 风度翩翩的程序猿 | 来源:发表于2023-06-27 10:43 被阅读0次

你可以使用 strtotime() 函数将时间戳转换为日期,并使用 date() 函数将日期格式化为年月。以下是一个示例代码:

$startTimestamp = 1609459200; // 开始时间戳
$endTimestamp = 1612137599; // 结束时间戳

// 将开始时间戳转换为年月
$startYearMonth = date('Y-m', $startTimestamp);

// 将结束时间戳转换为年月
$endYearMonth = date('Y-m', $endTimestamp);

// 获取范围内的年月
$yearMonths = [];
$currentYearMonth = $startYearMonth;
while ($currentYearMonth <= $endYearMonth) {
    $yearMonths[] = $currentYearMonth;
    $currentYearMonth = date('Y-m', strtotime($currentYearMonth . ' +1 month'));
}

// 输出范围内的年月
foreach ($yearMonths as $yearMonth) {
    echo $yearMonth . "\n";
}

在上面的示例中,我们先将开始时间戳和结束时间戳转换为年月格式。然后,通过循环的方式逐个生成范围内的年月。最后,我们通过 foreach 循环来输出这些年月。

相关文章

网友评论

      本文标题:php 两个时间戳开始和结束,获取范围内的年月

      本文链接:https://www.haomeiwen.com/subject/txfnydtx.html