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 8850861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:59:53+00:00 2026-06-14T12:59:53+00:00

I am using the following lines of code to get the dates I am

  • 0

I am using the following lines of code to get the dates I am trying to generate for some reports and it seems to work fine except in a few instances and I can’t see why that would be.

// what week numbers belong to which period
$adminconfig_periods = array(
        1=>array(1,2,3,4),
        2=>array(5,6,7,8,9),
        3=>array(10,11,12,13),
        4=>array(14,15,16,17),
        5=>array(18,19,20,21,22),
        6=>array(23,24,25,26),
        7=>array(27,28,29,30),
        8=>array(31,32,33,34,35),
        9=>array(36,37,38,39),
        10=>array(40,41,42,43),
        11=>array(44,45,46,47,48),
        12=>array(49,50,51,52,53)
    );

    /**
     * Get period no for week no
     *
     * @param string week the week 
     * @return int - the period no
     */             
     function getPeriodForWeek($week) {
        global $adminconfig_periods;
        $foundperiod = false;
        $period = 1;
        while(!$foundperiod) {
            if(in_array($week, $adminconfig_periods[$period])) {
                $foundperiod = true;
            } else {
                $period ++; 
            }
        }
        return $period;
    }


    $wA        = $_GET['wA'];
    $yA        = $_GET['yA'];


    $prev_period = '';
    $next_period = '';



    if (!isset($wA) || !isset($yA)) {
        // period and year aren't set so do it for current period

        // period starts on first Sunday of the first week in the period and ends on the last Saturday of the last week in the period
        $week = date('W');
        $period = getPeriodForWeek($week);

        $wA  = date('m');
        $yA  = date('Y');

    }
    else {
        // period and year are set

        // period starts on first Sunday of the first week in the period and ends on the last Saturday of the last week in the period

        $period = $wA;
    }


        // get date of first Sunday of the first week in this period
        $period_start_week = $adminconfig_periods[$period][0];


        // get the Sunday of this week
        $period_start = date('Y-m-d', strtotime($yA  . 'W' . $period_start_week  . '0'));


        // get date of last Saturday of the last week in this period
        $period_length = count($adminconfig_periods[$period]);
        // array indexes start from 0 so we need to take one off this value
        $last_element = $period_length - 1;

        $period_end_week = $adminconfig_periods[$period][$last_element];


        // get the Saturday of this week
        $period_end = date('Y-m-d', strtotime($yA  . 'W' . $period_end_week  . '6'));           

On this page I have some select menu controls for changing the period and year number and when it gets to certain periods I get some bizarre dates.

The periods are quite confusing but if anybody has any questions then feel free to ask.

Period | What it Should Be       | Actual Result
    11 | 28/10/2012 - 01/12/2012 | 28/10/2012 - 01/12/2012
    10 | 30/09/2012 - 27/10/2012 | 30/09/2012 - 27/10/2012
    09 | 02/09/2012 - 29/09/2012 | 02/09/2012 - 29/09/2012
    08 | 29/07/2012 - 01/09/2012 | 29/07/2012 - 01/09/2012
    07 | 01/07/2012 - 28/07/2012 | 01/07/2012 - 28/07/2012
    06 | 03/06/2012 - 30/06/2012 | 03/06/2012 - 30/06/2012
    05 | 29/04/2012 - 02/06/2012 | 29/04/2012 - 02/06/2012
    04 | 01/04/2012 - 28/04/2012 | 01/04/2012 - 28/04/2012
    03 | 04/03/2012 - 31/03/2012 | 04/03/2012 - 31/03/2012
    02 | 29/01/2012 - 03/02/2012 | 10/12/2012 - 01/01/1970
    01 | 01/01/2012 - 28/01/2012 | 05/03/2012 - 12/11/2012

As you can see, it seems to go insane for periods 1 and 2 for some reason and I don’t understand why.

The parameters are passed in the following way: ?wA=1&yA=2012 and if those are not set it uses the current month and period.

If I was to guess then I’d say it might have something to do with leap years but I thought the code would be able to handle that automatically? Who knows, hopefully a few extra pairs of eyes will spot something stupid I’ve missed.

Code that echo’s the dates

date("d/m/Y", strtotime($period_start)) . ' - ' . date("d/m/Y", strtotime($period_end)) .')';
  • 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-14T12:59:54+00:00Added an answer on June 14, 2026 at 12:59 pm

    The error is from those 2 lines

    $period_start = date('Y-m-d', strtotime($yA  . 'W' . $period_start_week  . '0'));
    $period_end = date('Y-m-d', strtotime($yA  . 'W' . $period_end_week  . '6'));
    

    if the week number is 2 for example, adding a 6 to it, it would be 26 (week 26), so you have to add a zero to the left side for the single digit ones. Let’s do that with str_pad():

    $period_start = date('Y-m-d', strtotime($yA  . 'W' . str_pad($period_start_week, 2, 0, STR_PAD_LEFT) . '0'));
    $period_end = date('Y-m-d', strtotime($yA  . 'W' . str_pad($period_end_week, 2, 0, STR_PAD_LEFT) . '6'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following lines of code to download and save an html
I'm using the following lines of code in my .htaccess file to create redirects.
Given the following lines of code which is using JQTOUCH: $('#customers').bind('pageAnimationEnd', function(e, info){ if
Using LINQ on collections, what is the difference between the following lines of code?
I'm trying to pre-populate a wxPython DatePicker with a value using the following code:
I'm using the following lines of code to convert the unix time I'm getting
I use the following code to generate a spam report using SpamAssassin: use Mail::SpamAssassin;
I am using the following line of code to download the contents of an
I am using the following line of code to save my file of yoyo.txt
I'm using the following line of code to run my php file that I

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.