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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:52:19+00:00 2026-05-22T23:52:19+00:00

My dilemma is that if I request more than 6 months or so (

  • 0

My dilemma is that if I request more than 6 months or so ( I do not know the approximate number ) from my webservices ( which gets called via JS ), I get nothing back. In other words, I have to limit it to 6 months.

So let’s consider this scenario:

$a = strtotime('June 3, 2011'); 
$b = strtotime('June 3, 2012');

I need to split this up by 6 months in order to make 2 distinct web servicerequests so that each call requests 6 months.

So with $a and $b, I need to split these up into as many date ranges as possible when taking the amount of months total divided by 6.

The first date range I need is from June 1, 2011 to November 31, 2011. The second date range I need is from December 1, 2011 to July 1, 2012.

The idea I had in mind was finding the number of months, then if it was greater than the limit variable 6, do a loop and increment the initial date by 6 months per loop.

Pseudo-code ( I actually initially wrote it in JS but figured it’d be easier to do in PHP because I wouldn’t have to deal with multiple asynchronous request behaviour ):

   var numMonths = monthDiff ( a, b ), ret = [], limit = 6, loopLimit = Math.ceil( numMonths / limit ), ranges = [];

    if ( numMonths > limit ) {

        for ( var i = 0; i<loopLimit; i++ ) {
            var start = new Date(b);
            var end = new Date ( b.setMonth( b.getMonth() + limit ) );
            ranges.push( start, end );
        }
    }

Does anyone know of a succinct way of doing this? Can anyone spot any programmatic flaws in this?

  • 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-22T23:52:20+00:00Added an answer on May 22, 2026 at 11:52 pm

    Try:

    $a = strtotime("June 3, 2011 00:00:00Z"); 
    $b = strtotime("June 3, 2012 00:00:00Z");
    fetchAll($a,$b);
    
    function fetchAll($a,$b) {
      $fetchLimit = "6 months";  // or, say, "180 days"; a string
    
      if ($b <= strtotime(gmdate("Y-m-d H:i:s\Z",$a)." +".$fetchLimit)) {
        // it fits in one chunk
        fetchChunk($a,$b);
      }
      else {  // chunkify it!
        $lowerBound = $a;
        $upperBound = strtotime(gmdate("Y-m-d H:i:s\Z",$a)." +".$fetchLimit);
        while ($upperBound < $b) { // fetch full chunks while there're some left
          fetchChunk($lowerBound,$upperBound);
          $lowerBound = $upperBound;
          $upperBound = strtotime(gmdate("Y-m-d H:i:s\Z",$lowerBound)." +".$fetchLimit);
        }
        fetchChunk($lowerBound,$b); // get last (likely) partial chunk
      }
    
    }
    
    function fetchChunk($a,$b) {
      /* insert your function that actually grabs the partial data */
      //
      // for test, just display the chunk range:
      echo gmdate("Y-m-d H:i:s\Z",$a)." to ".gmdate("Y-m-d H:i:s\Z",$b)."<br>";
    }
    

    …where $fetchLimit in fetchAll() is any duration string parseable by strtotime(). You could then keep appending the output of each fetchChunk() to an initially blank variable which is later returned by fetchAll().

    This example fetches two six-month “chunks”, as expected. Changing $b to one day later adds a third chunk containing only that extra day:

    2011-06-03 00:00:00Z to 2011-12-03 00:00:00Z
    2011-12-03 00:00:00Z to 2012-06-03 00:00:00Z
    2012-06-03 00:00:00Z to 2012-06-04 00:00:00Z
    

    Of course, PHP 5.3 has somewhat more elegant time functions like DateTime::diff, but the code above should work fine in PHP 5.2.x.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dilemma whereby I have a form which contains a number of
I have the following dilemma, I have an Android Application that POST a request
I have a dilemma that I've encountered before. What's the best in terms of
The dilemma I'm using a before_filter in my controller that restricts access to admins.
I'm running into a bit of dilemma at the moment in that I need
I ran into an interesting dilemma today. I have a function that handles information
I'm facing a dilemma (!). In a first scenario, I implemented a solution that
I have an interesting dilemma to which, I hope, someone may have an answer.
My dilemma is that I need to compile a large amount of projects and
I'm having a dilemma. I have a field hashedX that is a hashed/salted value

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.