Calculating week of specified date.
- by Louis W
Does PHP calculate weeks as being Sunday - Saturday? For a given date I am trying to determine the beginning and ending date of it's week as well as the beginning date of the next/previous weeks. Everything works fine unless I pass in a Sunday and it thinks the date is in a previous week.
$start = $_GET['start'];
$year = date('Y', strtotime($start));
$week = date('W', strtotime($start));
$sunday = strtotime($year.'W'.$week.'0');
$next = strtotime('+7 Days', $sunday);
$prev = strtotime('-7 Days', $sunday);
echo '<p>week: ' . $week . '</p>';
echo '<p>sunday: ' . date('Y-m-d', $sunday) . '</p>';
echo '<p>next:' . date('Y-m-d', $next) . '</p>';
echo '<p>prev: ' . date('Y-m-d', $prev) . '</p>';
Outcome:
2011-01-09 (Sunday)
Week: 01
WRONG
2011-01-10 (Monday)
Week: 02
RIGHT
2011-01-15 (Saturday)
Week: 02
RIGHT