我需要获得给定周的日期列表,其中包含该周的一个日期和开始日期。开始日可能是周一,周日等。
答案 0 :(得分:1)
这是我认为有效的方式,不确定它是最有效的方式。 $ weekStart应设置为您认为本周的第一天(0 =星期日,1 =星期一等),$ date是输入日期。
$weekStart = 0;
$date = '2011-06-1';
$timestamp = strtotime($date);
$dayOfWeek = date('N', $timestamp);
$startDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + $weekStart, date('Y', $timestamp));
$endDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + 6 + $weekStart, date('Y', $timestamp));
echo 'Week runs from ' . date('Y-m-d', $startDate) . ' to ' . date('Y-m-d', $endDate);
答案 1 :(得分:0)
替换$ dayOfWeek = date('N',$ timestamp); $ dayOfWeek = date('w',$ timestamp);因为如果使用星期天它会返回错误的星期日期。 使用$ date ='2013-07-28'进行测试。
$weekStart = 0;
$date = '2011-06-1';
$timestamp = strtotime($date);
$dayOfWeek = date('w', $timestamp);
$startDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + $weekStart, date('Y', $timestamp));
$endDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + 6 + $weekStart, date('Y', $timestamp));
echo 'Week runs from ' . date('Y-m-d', $startDate) . ' to ' . date('Y-m-d', $endDate);