Reputation: 1960
I'm trying to determine the week ranges from Monday - Sunday with PHP. I'm able to calculate the week number but I can't ssem to get the correct ending week range for each week.
Here is what I have so far:
$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);
}
Any idea what I'm missing or is there an easier way?
Upvotes: 1
Views: 171
Reputation: 95121
You can try
$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);
Upvotes: 2