PHP 获取上周、上上周、上上上周和上上上上周的时间段
你可以使用 PHP 的 strtotime 函数结合 date 函数来获取上周、上上周、上上上周和上上上上周的时间段。以下是一个示例代码:
// 获取上周时间段
$lastWeekStart = strtotime('last week', strtotime('monday this week'));
$lastWeekEnd = strtotime('sunday this week', $lastWeekStart);
// 获取上上周时间段
$twoWeeksAgoStart = strtotime('last week', $lastWeekStart);
$twoWeeksAgoEnd = strtotime('sunday this week', $twoWeeksAgoStart);
// 获取上上上周时间段
$threeWeeksAgoStart = strtotime('last week', $twoWeeksAgoStart);
$threeWeeksAgoEnd = strtotime('sunday this week', $threeWeeksAgoStart);
// 获取上上上上周时间段
$fourWeeksAgoStart = strtotime('last week', $threeWeeksAgoStart);
$fourWeeksAgoEnd = strtotime('sunday this week', $fourWeeksAgoStart);
// 格式化日期
$lastWeekStart = date('Y-m-d', $lastWeekStart);
$lastWeekEnd = date('Y-m-d', $lastWeekEnd);
$twoWeeksAgoStart = date('Y-m-d', $twoWeeksAgoStart);
$twoWeeksAgoEnd = date('Y-m-d', $twoWeeksAgoEnd);
$threeWeeksAgoStart = date('Y-m-d', $threeWeeksAgoStart);
$threeWeeksAgoEnd = date('Y-m-d', $threeWeeksAgoEnd);
$fourWeeksAgoStart = date('Y-m-d', $fourWeeksAgoStart);
$fourWeeksAgoEnd = date('Y-m-d', $fourWeeksAgoEnd);
// 输出时间段
echo "上周:$lastWeekStart - $lastWeekEnd\n";
echo "上上周:$twoWeeksAgoStart - $twoWeeksAgoEnd\n";
echo "上上上周:$threeWeeksAgoStart - $threeWeeksAgoEnd\n";
echo "上上上上周:$fourWeeksAgoStart - $fourWeeksAgoEnd\n";
上述代码会输出上周、上上周、上上上周和上上上上周的时间段,每周的第一天和最后一天的日期。你可以根据需要进行进一步的处理和格式化输出。
原文地址: https://www.cveoy.top/t/topic/pLlw 著作权归作者所有。请勿转载和采集!