PHP 获取本周每日的起始和结束时间戳
以下是一个 PHP 函数,可以获取本周(周一至周日)每天的起始时间戳(当天00:00:00)和结束时间戳(当天23:59:59)
function getWeekDayTimestamps() { $weekDays = []; $current = strtotime('monday this week'); for ($i = 0; $i < 7; $i++) { $dayStart = strtotime(date('Y-m-d 00:00:00', $current)); $dayEnd = strtotime(date('Y-m-d 23:59:59', $current)); $weekDays[] = [ 'day' => date('l', $current), 'date' => date('Y-m-d', $current), 'start' => $dayStart, 'end' => $dayEnd ]; $current = strtotime('+1 day', $current); } return $weekDays; } // 使用示例 $weekTimestamps = getWeekDayTimestamps(); foreach ($weekTimestamps as $day) { echo "{$day['day']} ({$day['date']}): "; echo "开始于 " . date('Y-m-d H:i:s', $day['start']) . " ({$day['start']}), "; echo "结束于 " . date('Y-m-d H:i:s', $day['end']) . " ({$day['end']})\n<br>"; }
发表评论