1

我正在尝试使用 PHP 确定从周一到周日的周范围。我能够计算周数,但我无法获得每周正确的结束周范围。

这是我到目前为止所拥有的:

$start_date = 1337621424;
$end_date = 1349964545;

$start_week = (int)date('W', $start_date); 
$end_week = (int)date('W', $end_date); 

echo "START WEEK: " . $start_week . "<BR>";
echo "END WEEK: " . $end_week . "<BR>";

$dates = array();

if ($start_week && $end_week) {

    for ($week=$start_week; $week<=$end_week; $week++) {
        echo "WEEK: " . $week . "<BR>";

        $startdate = strtotime ( '- ' . $week. ' week', strtotime ( date('Y-m-d') ) ) ;
        $startdate = date ( 'Y-m-d' , $startdate );

        //$end = date('Y-m-d');
        $end = strtotime ( '- ' . ($week + 1) . ' week', strtotime ( $startdate ) ) ;
        $end = date ( 'Y-m-d' , $end );

        $dates[$week]['start'] = $startdate;
        $dates[$week]['end'] = $end;
    }

    print_r($dates);
}   

知道我缺少什么或者有更简单的方法吗?

4

1 回答 1

2

你可以试试

$startDate = new DateTime();
$startDate->setTimestamp(1337621424);

$endDate = new DateTime();
$endDate->setTimestamp(1349964545);

$dates = array();
while ( $startDate <= $endDate ) {
    $week = array();
    $week['start'] = $startDate->format("Y-m-d");
    $startDate->modify("+1 week");
    $week['end'] = $startDate->format("Y-m-d");

    $dates[$startDate->format("W")] = $week ;
}
var_dump($dates);
于 2012-10-11T16:13:30.847 回答