Php-将时间段划分为相等的间隔
我必须创建一个函数,它获取 5 个参数,代表有关某个部门的工作时间的信息。
- 工作开始/结束时,
- 当午餐时间(或任何其他休息时间)开始/结束时,
- 表示分钟的整数,我们应该将时间段划分为多少小部分。
此外,工作时间可能没有休息时间。
该函数应返回工作时间的所有间隔。
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;
}
因此,如果我的函数有以下参数,
function split_time_into_intervals("8:30","14:50","11:45", "12:25");
我想检索以下数组:
$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
有什么建议吗?我会 apriciate 任何与此问题相关的 php(或 C#)代码!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这可能有点无效,但你可以尝试这个(我没有测试它,它可能有一些错误,但它应该工作类似:
当然,
$time += 60
应该翻译为实际的分钟添加,这是一个伪算法,希望它有帮助。
Well, this might be a bit uneffective, but you could try this (I didn't test it, and it might have some errors, but it should work something alike:
of course,
$time += 60
should be translated to actual minute adding. This is a pseudoalgorithm.Hope it helps
终于我解决了我的问题!谢谢塔昆的兴趣!
此刻它按照我想要的方式工作了!我想在某些情况下这个函数可能会出现一些错误,所以我想知道它们是否会出现。
现在有两个函数。抱歉,如果它可以写得更短、更优化,但至少它有效!
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!