-1

Possible Duplicate:
Php.Advance weekly calendar one week

I have written a script that displays a calendar week by week that the user can chose to go back or forward a week at a time. Everything works great except the first week of every year still displays the wrong year and the 31st December shows as 02/01. It seems that only week 1 is affected, the days are correct again in week and onwards

<?
if(isset($_POST['add_week'])){
     $last_week_ts = strtotime($_POST['last_week']);
     $display_week_ts = $last_week_ts + (3600 * 24 * 7);
} else if (isset($_POST['back_week'])) {
     $last_week_ts = strtotime($_POST['last_week']);
     $display_week_ts = $last_week_ts - (3600 * 24 * 7);
} else {
    $display_week_ts = floor(time() / (3600 * 24)) * 3600 * 24;
}

    $week_start = date('d-m-Y', $display_week_ts);
    $week_number = date("W", $display_week_ts);
    $year = date("Y", $display_week_ts);

echo $week_start.' '.$week_number.' '.$year;
?>

<table name="week">
    <tr>
<?
for($day=1; $day<=7; $day++)
{
    echo '<td>';
    echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
    echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" /><input type="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>

Any help will be appreciated

Community
  • 1
  • 1
tatty27
  • 1,553
  • 4
  • 34
  • 73
  • Which line are you referring to? You should really keep this under you previously asked question, not open a new one. Also you didn't change the echo part in the loop as suggested in the original question. – Mike Brant Sep 04 '12 at 17:27
  • sorry, I never saw the added code underneath. Thanks for your help in this – tatty27 Sep 04 '12 at 17:36

2 Answers2

0

This has to do with the format you are providing date(). Specifically W.

From the PHP Docs:

ISO-8601 week number of year, weeks starting on Monday

It is coincidence this is appearing because December 31, 2012 is a Monday.

You should rewrite your code to use other ways to calculate dates. I'd recommend against trusting form data.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
0

As suggested in the answer to the original question, you need to not use the week number and year number stuff.

If at the end of the day, you want to be able to list seven days for the week, just do not use this section of code at all:

$week_number = date("W", $display_week_ts);
$year = date("Y", $display_week_ts);

echo $week_start.' '.$week_number.' '.$year;

And replace the loop that echos out with the loop suggested in the original question.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103