只使用PHP自带的date和strtotime获取各种时间戳
只使用PHP自带的date和strtotime获取今天起止时间戳,昨天起止时间戳,本周起止时间戳,本月起止时间戳,本季度起止时间戳,本年度起止时间戳,去年起止时间戳
// 获取今天的起始时间戳和结束时间戳 $today_start = strtotime('today'); $today_end = strtotime('tomorrow') - 1; // 获取昨天的起始时间戳和结束时间戳 $yesterday_start = strtotime('yesterday'); $yesterday_end = $today_start - 1; // 获取本周的起始时间戳和结束时间戳 $this_week_start = strtotime('last monday', strtotime('this week')); $this_week_end = strtotime('next sunday', strtotime('this week')) + 86399; // 获取本月的起始时间戳和结束时间戳 $this_month_start = strtotime('first day of this month'); $this_month_end = strtotime('last day of this month') + 86399; // 获取本季度的起始时间戳和结束时间戳 $this_quarter_start = strtotime(date('Y-m-d', mktime(0, 0, 0, (ceil(date('n') / 3) - 1) * 3 + 1, 1))); $this_quarter_end = strtotime(date('Y-m-d', mktime(0, 0, 0, (ceil(date('n') / 3)) * 3 + 1, 0))) + 86399; // 获取本年的起始时间戳和结束时间戳 $this_year_start = strtotime('first day of January ' . date('Y')); $this_year_end = strtotime('last day of December ' . date('Y')) + 86399; // 获取去年的起始时间戳和结束时间戳 $last_year_start = strtotime('first day of January ' . (date('Y') - 1)); $last_year_end = strtotime('last day of December ' . (date('Y') - 1)) + 86399; // 输出结果 echo "今天: " . date('Y-m-d H:i:s', $today_start) . " 至 " . date('Y-m-d H:i:s', $today_end) . "\n"; echo "昨天: " . date('Y-m-d H:i:s', $yesterday_start) . " 至 " . date('Y-m-d H:i:s', $yesterday_end) . "\n"; echo "本周: " . date('Y-m-d H:i:s', $this_week_start) . " 至 " . date('Y-m-d H:i:s', $this_week_end) . "\n"; echo "本月: " . date('Y-m-d H:i:s', $this_month_start) . " 至 " . date('Y-m-d H:i:s', $this_month_end) . "\n"; echo "本季度: " . date('Y-m-d H:i:s', $this_quarter_start) . " 至 " . date('Y-m-d H:i:s', $this_quarter_end) . "\n"; echo "本年: " . date('Y-m-d H:i:s', $this_year_start) . " 至 " . date('Y-m-d H:i:s', $this_year_end) . "\n"; echo "去年: " . date('Y-m-d H:i:s', $last_year_start) . " 至 " . date('Y-m-d H:i:s', $last_year_end) . "\n";