有没有办法在一个月的PHP中获取每周的日期开始/结束? 所以,例如,如果我这个月(2011年11月),我需要返回
30 Oct -> 5 Nov
6 Nov -> 12 Nov
13 Nov -> 19 Nov
20 Nov -> 26 Nov
27 Nov -> 3 Dec
LE
这是我所做的,以防其他人需要它。代码并不漂亮但它可以正常工作。
public function getMonthWeeks($month, $year)
{
$month = intval($month);
if ($month < 1) {
$month = 1;
}
if ($month > 12) {
$month = 12;
}
$tsfdOfMonth = mktime(0, 0, 0, $month, 1, $year);
$dayOfWeek = idate('w', $tsfdOfMonth);
$tIntervalStart = $tsfdOfMonth;
$tNextMonth = idate('m', strtotime("+1 month", $tsfdOfMonth));
if ($dayOfWeek > 0) {
$tStr = sprintf("-%d %s",
$dayOfWeek,
$dayOfWeek == 1 ? 'day' : 'days'
);
$tIntervalStart = strtotime($tStr, $tsfdOfMonth);
}
$resultDates = array();
$tsStart = $tIntervalStart;
$tsEnd = strtotime('+6 days', $tsStart);
while (true) {
$rObj = new stdClass;
$rObj->LinkStr = sprintf("%s %s - %s %s",
date('M', $tsStart), date('d', $tsStart),
date('M', $tsEnd), date('d', $tsEnd)
);
$rObj->DateStart = date('Y-m-d', $tsStart);
$rObj->DateEnd = date('Y-m-d', $tsEnd);
$resultDates[] = $rObj;
if (idate('m', strtotime('+1 day', $tsEnd)) == $tNextMonth) {
break;
}
$tsStart = strtotime('+1 day', $tsEnd);
$tsEnd = strtotime('+6 days', $tsStart);
}
return $resultDates;
}
答案 0 :(得分:4)
你必须写一个函数:
算法:
两项功能非常有用:$saturdaytimestamp = strtotime("+6 days", $sundaytimestamp)
和$dayofweek = idate('w', $timestamp);
答案 1 :(得分:2)
您应该使用这些功能 - cal_days_in_month,strtotime,date
$aMonthWeeks = get_month_week_day_ranges(2011, 11);
print_r( $aMonthWeeks );
function get_month_week_day_ranges ( $year, $month ) {
$last_month_day_num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$first_month_day_timestamp = strtotime($year.'-'.$month.'-01');
$last_month_daty_timestamp = strtotime($year.'-'.$month.'-'.$last_month_day_num );
$first_month_week = date('W', $first_month_day_timestamp);
$last_month_week = date('W', $last_month_daty_timestamp);
$aMonthWeeks = array();
for( $week = $first_month_week; $week <= $last_month_week; $week ++ ) {
echo sprintf('%dW%02d-1', $year, $week ), "\n";
array_push( $aMonthWeeks, array(
date("d:m:Y", strtotime( sprintf('%dW%02d-1', $year, $week ) ) ),
date("d:m:Y", strtotime( sprintf('%dW%02d-7', $year, $week ) ) ),
) );
}
return $aMonthWeeks;
}
对于我的语言环境(一周的第一天是星期一),结果是
Array
(
[0] => Array
(
[0] => 31:10:2011
[1] => 06:11:2011
)
[1] => Array
(
[0] => 07:11:2011
[1] => 13:11:2011
)
[2] => Array
(
[0] => 14:11:2011
[1] => 20:11:2011
)
[3] => Array
(
[0] => 21:11:2011
[1] => 27:11:2011
)
[4] => Array
(
[0] => 28:11:2011
[1] => 04:12:2011
)
)
答案 2 :(得分:1)
我不知道最好的方法是什么,但是这可能有用:你问PHP本周的哪一天是date('w', mktime(0, 0, 0, (int)date('m'), 1, (int)date('Y'));
这个月的第一天然后你可以倒数到第一天星期几,使用checkdate()
函数确定上个月的最后一天,然后使用简单的while()
构建数组,直到您达到一周结束但未在下个月开始。
答案 3 :(得分:0)
我只是花时间为你编写代码,这将输出你想要的内容:
<?php
/* CONFIG */
$year = 2011;
$month = 11;
/* CODE */
$first_day = mktime(0, 0, 0, $month, 1, $year);
$month_first_day = date('w', $first_day);
if($month_first_day != 1) {
$offset = $month_first_day - 1;
$previous_month = ($month == 1 ? mktime(0, 0, 0, 12, 1, ($year - 1)) : mktime(0, 0, 0, ($month - 1), 1, $year));
$previous_month_days = date('t', $previous_month);
$previous_month_saturday = $previous_month_days - $offset;
$previous_month_stamp = ($month == 1 ? mktime(0, 0, 0, 12, $previous_month_saturday, ($year - 1)) : mktime(0, 0, 0, ($month - 1), $previous_month_saturday, $year));
$first_saturday = $previous_month_stamp;
}else{
$first_saturday = $first_day;
}
$loops = date('t', $first_day);
$loops = round($loops / 7);
for($loop = 1; $loop < ($loops + 2); $loop++) {
echo date('j M', $first_saturday + ((7 * ($loop - 1)) * 24 * 60 * 60)) . ' -> ' . date('j M', $first_saturday + (((7 * $loop) - 1) * 24 * 60 * 60)) . '<br />';
}
这可能不是实现目标的最有效方式。然而,它得到了你要求完成的工作,它让你大致了解如何实现这一目标。