检查工作日是否在两个工作日之间

时间:2012-02-24 02:16:32

标签: php algorithm

我最近一直坚持我认为应该是一个相当简单的问题。

基本上我将工作日表示为代表星期一到星期日的数字(1-7)。

我需要一个具有这个原型的函数:

function weekday_between(weekday, start_weekday, end_weekday) {
    //if the weekday is between start and end then return true, otherwise return false
    ...
}

我遇到的主要问题是start_weekday大于end_weekday,因为我认为你不能只依赖嵌套的if语句(?)。

例如:

weekday_between(2, 1, 3) //return true
weekday_between(1, 7, 2) //return true, the hard part
weekday_between(2, 7, 1) //return false
weekday_between(6, 7, 1) //return false

该项目是在PHP中,但伪代码很好,甚至只是朝着正确方向的一点。

2 个答案:

答案 0 :(得分:4)

function weekday_between(weekday, start_weekday, end_weekday) {
    if (end_weekday < start_weekday)
        return (weekday > start_weekday) OR (weekday < end_weekday);
    else
        return (weekday > start_weekday) AND (weekday < end_weekday);
}

答案 1 :(得分:1)

此处:$ b =介于值之间,$ s =起始值,$ e =结束值。

function weekday_between($s, $b, $e){
  if($s>$e) $s -=7;
  return (($b>$s)&&($b<$e));
} 

是的,我改变了你的函数参数的顺序......是不是更有意义的是在...之间有值...