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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:15:21+00:00 2026-06-09T02:15:21+00:00

I am trying to determine the number of date intervals between two dates. The

  • 0

I am trying to determine the number of date intervals between two dates. The interval could be a number of months, days, hours or whatever the user has chosen.

At the moment I have a function using recursion, however, this will break if there have been more than 100 intervals between the two points, due to the recursive nesting limit.

private static function findCurrentInterval($initialStartTimestamp, $intervalStrtotimeFormat, $now = null, $period = 1)
{
    if (is_null($now)){
        $now = time();
    }

    $endOfCurrentInterval = strtotime($intervalStrtotimeFormat, $initialStartTimestamp);

    if ($now > $initialStartTimestamp && $now < $endOfCurrentInterval){
        return new self($period);
    }else{
        # increment the period ID tracking variable
        $period++;
        return self::findCurrentInterval($endOfCurrentInterval, $intervalStrtotimeFormat, $now, $period);
    }
}

All I need out of it is an integer value that represents which period of time intervals we are in currently, i.e. if it is set to a 7 day interval, and 20 days have passed between the start point and now, it would return ‘3’, because we’d be into the 3rd period.

Is there a way of doing this without recursion, perhaps using DateInterval?

N.B. this would be really straightforward if it was limited to days or hours – but it needs to support months, which can have variable lengths.

  • 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-09T02:15:23+00:00Added an answer on June 9, 2026 at 2:15 am

    This seems to work:

    function findCurrentInterval($number, $type, $start, $end){
    
        $dt1 = DateTime::createFromFormat('Y-m-d H:i:s', $start);
        $dt2 = DateTime::createFromFormat('Y-m-d H:i:s', $end);
        $diff = $dt1->diff($dt2);
    
        $numDays = (int) $diff->format('%a');
        $numMonths = (int) $diff->format('%m');
        $numYears = (int) $diff->format('%y');
        $numHours = (int) $diff->format('%h');
    
        if ($numYears > 0){
            $numMonths += ($numYears * 12);
        }
    
        $numHours += $numDays * 24;
    
    
        switch ($type){
            case 'hours':
                return floor($numHours / $number);
            break;
            case 'days':
                return floor($numDays / $number);
            break;
            case 'months':
                return floor($numMonths / $number);
            break;
            default:
                throw new Exception('Interval type '.$type.' not implemented.');
        }
    
    }
    

    Examples:

    echo findCurrentInterval(3, 'hours', '2012-01-01 00:00:00', '2012-08-01 00:00:00');
    echo findCurrentInterval(7, 'days', '2012-01-01 00:00:00', '2012-08-01 00:00:00');
    echo findCurrentInterval(1, 'months', '2012-01-01 00:00:00', '2012-08-01 00:00:00');
    

    Outputs:

    1704
    30
    7
    

    And:

    echo findCurrentInterval(3, 'hours', '2011-01-01 00:00:00', '2012-08-01 00:00:00');
    echo findCurrentInterval(7, 'days', '2011-01-01 00:00:00', '2012-08-01 00:00:00');
    echo findCurrentInterval(1, 'months', '2011-01-01 00:00:00', '2012-08-01 00:00:00');
    

    Outputs:

    4624
    82
    19
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to determine the number of leap year days between two different dates
I am trying to determine the number of days between 2 dates using LINQ
I'm trying to use [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:date] on two different dates to
I am trying to determine if any differences exist between a number of fields
I'm trying to determine the iPhone user's location using a CLLocationManager and CLGeocoder. My
I'm trying to determine whether or not a string contains a number. This doesn't
I am trying to determine document similarity between a single document and each of
I'm trying to determine the function for determining the number of unordered combinations with
In my xslt, I am trying to determine the number of distinct schedule_id values
I'm trying to determine the version number of my Silverlight application. Currently I am

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.