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

The Archive Base Latest Questions

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

I have to create a function which gets 5 parameters representing information about the

  • 0

I have to create a function which gets 5 parameters representing information about the working time of some department.
– when the work starts/ends,
– when the lunchtime (or any other break) starts/ends,
– integer representing minutes into how small pieces we should divide time period.

Besides – it’s possible that there are no breaks in the working time.

The function should return all intervals from working time.

function split_time_into_intervals($work_starts,$work_ends,$break_starts=null;
          $break_ends=null,$minutes_per_interval=60)
{
    $intervals=array();

    //all of the function code
    return $intervals;
}

So if I have the following parameters for the function

function split_time_into_intervals("8:30","14:50","11:45", "12:25");

I would like to retrieve the following array:

$intervals[0]['starts']="8:30";
$intervals[0]['ends']="9:30";
$intervals[1]['starts']="9:30";
$intervals[1]['ends']="10:30";
$intervals[2]['starts']="10:30";
$intervals[2]['ends']="11:30";
$intervals[3]['starts']="11:30";
$intervals[3]['ends']="11:45";

//this interval was smaller than 60 minutes - because of the break (which starts at 11:45)
$intervals[4]['starts']="12:25";//starts when the break ends
$intervals[4]['ends']="13:25"; // interval is again 60 minutes
$intervals[5]['starts']="13:25";
$intervals[5]['ends']="14:25";
$intervals[6]['starts']="14:25";
$intervals[6]['ends']="14:50";

//this period is shorter than 60 minutes - because work ends 

Any advises? I would apriciate any php (or C#) code regarding to this problem!

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

    Finaly I solve my problem! Thanks, tharkun for interest!
    At this moment it works as I want it to! I suppose that in some cases this function might work with some bugs, so I would like to know if they show up.
    For now here are two functions. Sory, if it could be written shorter and more optimized, but at least it works!

    //Author: Elvijs Kukša
    function splitTimeIntoIntervals($work_starts,$work_ends,$break_starts=null,$break_ends=null,$minutes_per_interval=60){
    $intervals=array();
    $time = date("H:i",strtotime($work_starts));
    $first_after_break=false;
    while( strtotime($time) < strtotime($work_ends)){
    // if at least one of the arguments associated with breaks is mising - act like there is no break
        if($break_starts==null || $break_starts==null )
        {
        $time_starts=date("H:i", strtotime($time));
        $time_ends=date("H:i", strtotime($time_starts."+$minutes_per_interval minutes"));
        }
    // if the break start/end time is specified
        else{
        if($first_after_break==true){//first start time after break
        $time=(date("H:i",strtotime($break_ends)));
        $first_after_break=false;
        }
        $time_starts=(date("H:i",strtotime($time)));
        $time_ends=date("H:i", strtotime($time_starts."+$minutes_per_interval minutes"));
    //if end_time intersects break_start and break_end times
        if(timesIntersects($time_starts, $time_ends, $break_starts, $break_ends))
        {
        $time_ends=date("H:i",strtotime($break_starts));
        $first_after_break=true;
        }
        }
        //if end_time is after work_ends
        if(date("H:i", strtotime($time_ends))>date("H:i",strtotime($work_ends)))
        {
        $time_ends=date("H:i",strtotime($work_ends));
        }
        $intervals[] = array( 'starts' =>$time_starts, 'ends' => $time_ends);
        $time=$time_ends;
    }
    return $intervals;
    }
    //time intersects if order of parametrs is one of the following 1342 or 1324
    
    
    function timesIntersects($time1_from,$time1_till, $time2_from, $time2_till){
        $out;
        $time1_st=strtotime($time1_from);
        $time1_end=strtotime($time1_till);
        $time2_st=strtotime($time2_from);
        $time2_end=strtotime($time2_till);
        $duration1 =$time1_end - $time1_st;
        $duration2 = $time2_end - $time2_st;
        $time1_length=date("i",strtotime($time1_till."-$time1_from"));
        if(
                (($time1_st<=$time2_st&&$time2_st<=$time1_end&&$time1_end<=$time2_end)
                ||
                ($time1_st<=$time2_st&&$time2_st<=$time2_end&&$time2_end<=$time1_end)
                &&
                ($duration1>=$duration2)
                )
                ||
                ( $time1_st<=$time2_st&&$time2_st<=$time1_end&&$time1_end<=$time2_end)
                &&
                ($duration1<$duration2)
                )
        {
            return true;
        }
    
    
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.