1

Possible Duplicate:
Get next/previous ISO week and year in PHP

I am trying to write a script that will display the days of a week in a table and that will advance a week if a button is clicked. I have managed to get it working up until the point where it reaches the end of the year and then the dates all go wrong. He is what I have so far...

 <?
     if(isset($_POST['add_week'])){
     $week = date('d-m-Y', strtotime($_POST['last_week']));
     $new_week =  strtotime ( '+1 week' , strtotime ( $week ) ) ;
     $new_week = date('d-m-Y', $new_week);

     $week_number = date("W", strtotime( $new_week));
     $year = date("Y", strtotime( $new_week));
  }else{

         $week_number = date("W");
         $year = date("Y");
  }

  if($week_number < 10){
      $week_number = "0".$week_number;
   }

  $week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
 echo $week.' '.$new_week.' '.$week_number;
?>

<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 ype="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>

Some of the values have been echo'd so I can check the values that are being passed are correct and I know I have probably taken extra steps I didn't need to but I am fairly new to this and wanted to make the code easier to folllow whilst I get it working. As I said, the add button works a treat until it hits new year.

Thanks

Ok, made some advancement, works fine until it gets to 2012 then it just runs through 2012 again rather than starting 2013

 <?
if(isset($_POST['add_week'])){
    $week = date('d-m-Y', strtotime($_POST['last_week']));
    $new_week =  strtotime ( '+1 week' , strtotime ( $week ) ) ;
    $new_week = date('d-m-Y', $new_week);


    $week_number = date("W", strtotime( $new_week));
    $year = date("Y", strtotime( $new_week));
}else if(isset($_POST['back_week'])){
    $week = date('d-m-Y', strtotime($_POST['last_week']));
    $new_week =  strtotime ( '-1 week' , strtotime ( $week ) ) ;
    $new_week = date('d-m-Y', $new_week);


    $week_number = date("W", strtotime( $new_week));
    $year = date("Y", strtotime( $new_week));
}else{

$week_number = date("W");
$year = date("Y");
}
/*if($week_number < 10){
   $week_number = "0".$week_number;
}*/
$week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
echo $week.' '.$new_week.' '.$week_number;
?>

<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>
Community
  • 1
  • 1
tatty27
  • 1,553
  • 4
  • 34
  • 73
  • [find the last week](http://www.phpeveryday.com/articles/PHP-Date-Time-Finding-The-Total-Number-of-Weeks-in-Year-P386.html) in the year, then make condition `if($week_number>$last_week_number) increase year number`. I would love to help, but date manipulations gives me headache :)) – Peter Sep 04 '12 at 15:17
  • What does $_POST['last_week'] look like? – Mike Brant Sep 04 '12 at 15:18
  • I've been struggling with this for 2 days so I'm with you there on the headache thing. Thanks for your help, I'll try it – tatty27 Sep 04 '12 at 15:19
  • I've rollback question to it's original version, Netbeans is going mad, when try to auto-indent that code... :( – Peter Sep 04 '12 at 15:21
  • it's the same as the 'add_week' but with '-1 week', I'll put up the code with the now added $_POST'back_week' – tatty27 Sep 04 '12 at 15:22

1 Answers1

3

In my opinion you are going to be way better served to make all your calculation based on a unix timestamp value and then convert to string only as needed for output. That way you don't have to deal with week number problems (i.e. week 0), you are not limited to having Monday be the first day of each week (as as is the basis of calculation in date("W")), and you won't have to make a bunch of hacks to look for edge conditions.

So assuming that $_POST['last_week'] is in your d-m-Y format something like this:

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);

For the part where you are looping through the week to display you can use something like this:

for ($i = 0; $i < 7; $i++) {
    $current_day_ts = $display_week_ts + ($i * 3600 *24);
    echo date('d-m-Y', $current_day_ts); 
}
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • Thanks for that but the line $display_week_ts = floor((now() / (3600 * 24)) * 3600 * 24; comes up with a syntax error? – tatty27 Sep 04 '12 at 15:34
  • Ok, I fixed that by closing the parentheses after 24 but now I get a fatal error Call to undefined function now() – tatty27 Sep 04 '12 at 15:38
  • @user1267224 Sorry about that. Was thinking in SQL terms :). I have fixed that line including proper closed parenthesis (you don't want it after the last 24, as you are trying to get the 00:00 timestamp for the current date. – Mike Brant Sep 04 '12 at 15:41
  • Sorry, all I get now is 01-01-1970 for every day – tatty27 Sep 04 '12 at 15:46
  • Ok, I've realised that's my fault, I'm not sure how to convert echo date('d-m-Y', strtotime($year."W".$week_number.$day)) to use $display_week_ts so it loops through the different days – tatty27 Sep 04 '12 at 16:02
  • @user1267224 I have added a second code section to my answer above that should help. – Mike Brant Sep 04 '12 at 16:50
  • Hi, I have just checked again and it seems that it starts the diary from the current day, ie yesterday it was showing 4th sept as the beginning of the week and now today it is showing 5th sept a – tatty27 Sep 05 '12 at 00:30