Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8515583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:11:18+00:00 2026-06-11T05:11:18+00:00

Possible Duplicate: Get next/previous ISO week and year in PHP I am trying to

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:11:19+00:00Added an answer on June 11, 2026 at 5:11 am

    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); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I use PHP to get the current year? 2009 this
Possible Duplicate: Get first day of week in PHP? Hi, I want to find
Possible Duplicate: Trying to get tables next to each other horizontal I have two
Possible Duplicate: What is best solution to get next and previous month from given
Possible Duplicate: How to get the name of each day in next month? I
Possible Duplicate: Get URL of ASP.Net Page in code-behind I'm trying to hold current
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: Based on date, get todays and the next two days values from
Possible Duplicate: Get the id of a the item that is clicked I have
Possible Duplicate: ASP.net get the next tuesday Given a day of the month, how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.