dqb14659 2015-03-06 21:55
浏览 65

PHP计算第一周/第二周/第三周的第一周跳过

I writing a script where it will populate a series of dates based on the criteria set between a start and finish time.

I basically using the relative formats in php dates function (http://php.net/manual/en/datetime.formats.relative.php).

My script first get the intervals based on user's selection:

//first get the interval
$event_repeatmonth = $_POST['event_repeatmonth'];   
if ($event_repeatmonth == 1){ $interval = 'first';}
if ($event_repeatmonth == 2){ $interval = 'second';}
if ($event_repeatmonth == 3){ $interval = 'third';}
if ($event_repeatmonth == 4){ $interval = 'fourth';}
if ($event_repeatmonth == 5){ $interval = 'fifth';}
if ($event_repeatmonth == 6){ $interval = 'last';}

$onthe_monday = $_POST['onthe_monday'];
$onthe_tuesday = $_POST['onthe_tuesday'];
$onthe_wednesday = $_POST['onthe_wednesday'];
$onthe_thursday = $_POST['onthe_thursday'];
$onthe_friday = $_POST['onthe_friday'];
$onthe_saturday = $_POST['onthe_saturday'];
$onthe_sunday = $_POST['onthe_sunday'];

if ($onthe_monday == 1){ $interval_period[] = $interval." Monday";}                         
if ($onthe_tuesday == 1){ $interval_period[] = $interval." Tuesday";}               
if ($onthe_wednesday == 1){ $interval_period[] = $interval." Wednesday";}               
if ($onthe_thursday == 1){ $interval_period[] = $interval. " Thursday";}                
if ($onthe_friday == 1){ $interval_period[] = $interval. " Friday";}                
if ($onthe_saturday == 1){ $interval_period[] = $interval. " Saturday";}                
if ($onthe_sunday == 1){ $interval_period[] = $interval. " Sunday";}

Then it will loop through the dates within the start and finish date. It will grab the first date of the month as the reference date and find the corresponding 1st/2nd/3rd Monday/Tuesday... of the month:

//loop through to insert the array
while ($tmp <= $event_udate){
    $tmpmonth = date("Y-m", strtotime($tmp))."-01";

    //get proper date based on the intervals we are looking for
    foreach ($interval_period  AS $period){
        $tmp = date("Y-m-d", strtotime($period, strtotime($tmpmonth)));
        echo $period."--".$tmp."--".$tmpmonth."<br/>";
        //sometimes the first interval will be before the start date, skip that
        if ($tmp >= $event_sdate){
            $event_datearray[] = $tmp;
        }                       
    }                   
    //reset if reach end of the month
    if ($repeat_every > 1){ 
        //go to next month      
        $tmp = date('Y-m-d', strtotime('+'. $repeat_every.' month',  strtotime($tmp)));                     
    }else{                              
        //go to next month      
        $tmp = date('Y-m-d', strtotime('+1 month', strtotime($tmp)));               
    }           
}   

For example, here I asked the program to get the second Wednesday and the second Sunday of the month for every 3 months between the time frame of 4/1/2015 - 8/30/2015.

The result should have returned: 4/8, 4/12, 7/8, and 7/12. However, it appears that since 4/1 and 7/1 are on Wednesday, it skipped the first week and grabbed the 3rd Wednesday instead.

My echo result looks like below, as you can see, it grabbed 4/15 as the 2nd Wednesday of 2015-04-01.... and grabbed 7/15 as the 2nd Wednesday of 2015-07-01...

second Wednesday--2015-04-15--2015-04-01
second Sunday--2015-04-12--2015-04-01
second Wednesday--2015-07-15--2015-07-01
second Sunday--2015-07-12--2015-07-01
array(4) { [0]=> string(10) "2015-04-15" [1]=> string(10) "2015-04-12" [2]=> string(10) "2015-07-15" [3]=> string(10) "2015-07-12" }

I am pretty lost here. This seems to be issue only when the first of the month matches one of the criteria. If I change it to 2nd Thursday and 2nd Sunday, then it will return properly:

second Thursday--2015-04-09--2015-04-01
second Sunday--2015-04-12--2015-04-01
second Thursday--2015-07-09--2015-07-01
second Sunday--2015-07-12--2015-07-01
array(4) { [0]=> string(10) "2015-04-09" [1]=> string(10) "2015-04-12" [2]=> string(10) "2015-07-09" [3]=> string(10) "2015-07-12" }

Does anyone have the same issue before???

  • 写回答

1条回答 默认 最新

  • dongqia3502 2015-03-06 22:19
    关注

    I figured this out after I posted it....

    It turns out the relative format in PHP date DOES NOT include the date it refers to. For example, if you are looking for the 2nd Monday of 2015-04-01, it will look for the first instance of the 2nd Monday after 2015-04-01...

    So to fix this, I just check to see if the first of the month matches the criteria I am looking for, if it does, then change my reference day to the day before so it will include the original date into consideration.

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用