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

  • Home
  • SEARCH
  • 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 3239194
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:56:29+00:00 2026-05-17T17:56:29+00:00

I set out to generate a array containing a 12-month calendar starting on this

  • 0

I set out to generate a array containing a 12-month calendar starting on this (now) month. This is for a particular application, and requires special code, so I cannot use calendar libraries.

Here’s the code I have:

header('Content-type: text/plain');

$Cal1 = array();

$now = new fTimestamp('now');
$now = $now->modify('Y-m-1 00:00:00');

for ( $i = 0; $i < 12; $i++ ) {
 if ( $i > 1 ) {
  $then = $now->adjust("+$i months");
 }
 elseif ( $i == 1 ) {
  $then = $now->adjust("+1 month");
 }
 else {
  $then = $now;
 }
 $thisMonth = $then->format('F');
 $Cal1[$thisMonth] = array();
 $thisMonthDays = $then->format('t');
 for ( $j = 0; $j < $thisMonthDays; $j++ ) {
  if ( $i > 1 ) {
   $then = $then->adjust("+$i days");
  }
  elseif ( $i == 1 ) {
   $then = $then->adjust("+1 day");
  }
  $thisDate = $then->format('j');
  $thisDay = $then->format('l');
  $Cal1[$thisMonth][$thisDate] = $thisDay;
 }
}

var_dump($Cal1);

This should generate an array of the form:

array {
    ["Month_Name"] => array {
        [Day_Number] => "Day_Name"
        etc...
    }
    etc...
}

The script outputs the right number of months, but not the right number of days… The full dump is quite lengthy, so I’ll only post October, February, and March:

array(12) {
  ["October"]=>
  array(1) {
    [1]=>
    string(6) "Friday"
  }
  ["February"]=>
  array(22) {
    [5]=>
    string(8) "Saturday"
    [9]=>
    string(9) "Wednesday"
    [13]=>
    string(6) "Sunday"
    [17]=>
    string(8) "Thursday"
    [21]=>
    string(6) "Monday"
    [25]=>
    string(6) "Friday"
    [1]=>
    string(7) "Tuesday"
    [29]=>
    string(7) "Tuesday"
    [2]=>
    string(8) "Saturday"
    [6]=>
    string(9) "Wednesday"
    [10]=>
    string(6) "Sunday"
    [14]=>
    string(8) "Thursday"
    [18]=>
    string(6) "Monday"
    [22]=>
    string(6) "Friday"
    [26]=>
    string(7) "Tuesday"
    [30]=>
    string(8) "Saturday"
    [4]=>
    string(9) "Wednesday"
    [8]=>
    string(6) "Sunday"
    [12]=>
    string(8) "Thursday"
    [16]=>
    string(6) "Monday"
    [20]=>
    string(6) "Friday"
    [24]=>
    string(7) "Tuesday"
  }
  ["March"]=>
  array(19) {
    [6]=>
    string(6) "Sunday"
    [11]=>
    string(6) "Friday"
    [16]=>
    string(9) "Wednesday"
    [21]=>
    string(6) "Monday"
    [26]=>
    string(8) "Saturday"
    [31]=>
    string(8) "Thursday"
    [5]=>
    string(8) "Thursday"
    [10]=>
    string(7) "Tuesday"
    [15]=>
    string(6) "Sunday"
    [20]=>
    string(6) "Friday"
    [25]=>
    string(9) "Wednesday"
    [30]=>
    string(6) "Monday"
    [4]=>
    string(6) "Monday"
    [9]=>
    string(8) "Saturday"
    [14]=>
    string(8) "Thursday"
    [19]=>
    string(7) "Tuesday"
    [24]=>
    string(6) "Sunday"
    [29]=>
    string(6) "Friday"
    [3]=>
    string(9) "Wednesday"
  }

Now, what’s the matter?

  • 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-05-17T17:56:30+00:00Added an answer on May 17, 2026 at 5:56 pm

    Doesn’t directly answer your question, but I’d do it a lot simpler like so:

    $cursor = mktime(0, 0, 0, date('m'), 1);
    $end = strtotime('+1 year', $cursor);
    
    $out = array();
    
    while ($cursor < $end) {
        $out[date('F', $cursor)][date('j', $cursor)] = date('l', $cursor);
        $cursor = strtotime('+1 day', $cursor);
    }
    
    var_dump($out);
    

    To directly answer your question, you’re always adjusting $then relative to itself with increasingly bigger numbers, hence you’re skipping days and are getting funky numbers.

     for ( $j = 1; $j < $thisMonthDays; $j++ ) {
         $then = $then->adjust("+$i days");
     }
    
    1. $then is the 1st, you add 1 (first iteration)
    2. $then is the 2nd, you add 2 (second iteration)
    3. $then is the 4th, you add 3
    4. $then is the 7th, you add 4
    5. etc pp.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I set out this morning the task seemed simple: build a list of
I cannot figure out how to set an array to one of two sets
Given an array, unsigned char q[32]=1100111... , how can I generate a 4-bytes bit-set,
For a mobile application I have to generate a 1-bit-bitmap out of a 24-bit-bitmap.
I have this code which echoes me information from the database: <?php include('db_connect.php'); mysql_query(SET
I set out to get myself a neat C debugging macro, not really sure
I have a model set out like so: class Rating # user_id, author_id end
following a discussion in a software meeting I've set out to find out if
I need to set time out for the Http Request we make to a
I have a program with a set of input and a set of out

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.