dongpan2788 2015-12-29 11:17
浏览 34
已采纳

循环中的日期更改

Hi I've created an array that has a Start Date that increases by a month and this happens by how many months there are. So if my StartDate was 10/10/15 and placed 3 into my for loop I would receive 10/10/15 10/11/15 10/12/15 10/01/16. My current problem is that I've added a new while loop to find out if some of the days of the months fall on either a Saturday or Sunday.Which I don't want I would like it to fall on a weekday. So ive created this loop and it works however I have another problem. If 10/11/15 fell on a Sunday my loop will change it to 11/11/15 Monday which is correct but the rest of dates after it will now follow the changed date example be 11/12/15, 11/01/16.

I want it change the date but still the other dates to still increase from the startdate (10/10/15) like 10/10/15, 11/11/15, 10/12/15, 10/01/16. How would I do this? I also understand some of my code may not be the best but I am just a beginner. Any help would be much appreciated.

$date = "2015-10-1"; //startdate
$lol = 2; //length of loan
$start = strtotime($date); //startdate
$currentdate = $start;



echo "<pre>";
$times_table = [];
$titles[] = [
    'Version' => 'Version 5 ',
    'Date' => 'Feb-16',
    'Name' => 'gary',
    'RandNum' => '80',
    'PaymentType' => 'P',
    'dtType' => 'Q',

];


$times_table = [];
for($i = 0; $i <= ($lol + 1) ; $i++){
    $times_table[$i]['StartDate']=  $date   ;
    $times_table[$i]['Version']=  "v10" ;

    $cur_date = date("M-y-D", $currentdate);
    $times_table[$i]['DtMy']= "<strong>" . $cur_date . "</strong>" ;


            $mthYr = date("M", $currentdate);
            $nxtmth = " ";

// Loop that changes the day to a weekday   
    while($nxtmth != "Y" ) {

        if( $cur_date = date("M", $currentdate) !== $mthYr){

            $nxtmth = "Y";
            $currentdate = strtotime('-1 days', $currentdate);
            $cur_date = date("d-M", $currentdate);
            $times_table[$i]['DtMy']= "<strong>" . $cur_date . "</strong>" ;


           }


          if ($cur_date = date("w", $currentdate) == 0 || $cur_date = date("w", $currentdate) == 6){

             if ($nxtmth == "Y"){

                 $currentdate = strtotime('-1 day', $currentdate);
                 $cur_date = date("d-M", $currentdate);
                 $times_table[$i]['DtMy']= "<strong>" . $cur_date . "</strong>" ;

                    }       

             else{

                 $currentdate = strtotime('+1 day', $currentdate);  
                 $cur_date = date("d-M", $currentdate);
                 $times_table[$i]['DtMy']= "<strong>" . $cur_date . "</strong>" ;

                 }
             if(($cur_date = date("M", $currentdate)) !== $mthYr){

                 $nxtmth = "Y";
                 $currentdate = strtotime('-1 day', $currentdate);
                 $cur_date = date("d-M", $currentdate);
                 $times_table[$i]['DtMy']= "<strong>" . $cur_date . "</strong>" ;

                }        


            }

        if($cur_date = date("w", $currentdate) > 0 && $cur_date = date("w", $currentdate) < 6 ){
                     $cur_date = date("d-M", $currentdate);
                    $times_table[$i]['DtMy']= "<strong>" .  $cur_date . "</strong>" ; 
                    break;
        }


    }  


        $currentdate = strtotime('+1 month', $currentdate); //Adds 1 month



} 


print_r($times_table);

echo "</ pre>";
  • 写回答

1条回答 默认 最新

  • doushi3715 2015-12-29 12:01
    关注

    You can simply store the value in temp variable before overwriting

    <?php
    $date = "2015-10-1"; //startdate
    $lol = 2; //length of loan
    $dates = [];
    for($i = 0; $i <= ($lol + 1) ; $i++){
        $rawDate = strtotime($date);
        $tmpRawDate = $rawDate;
        $day    = date('N', $rawDate);
        // check if date is sat or sun
    
        if($day == 6 || $day == 7)
            $rawDate = strtotime($date . ' +'.(8 - $day).' day');
    
        $dates[] = date('Y-m-d', $rawDate);
        $rawDate = $tmpRawDate;
    
        $rawDate = strtotime($date . ' +1 month');
        $date = date('Y-m-d', $rawDate);
    }
    echo '<pre>';
    print_r($dates);
    

    Demo: https://eval.in/494664

    UPDATE

    <?php
    $date = "2015-10-1"; //startdate
    $lol = 2; //length of loan
    $start = strtotime($date); //startdate
    $currentdate = $start;
    
    echo "<pre>";
    $titles[] = [
        'Version' => 'Version 5 ',
        'Date' => 'Feb-16',
        'Name' => 'gary',
        'RandNum' => '80',
        'PaymentType' => 'P',
        'dtType' => 'Q',
    ];
    
    $times_table = [];
    for($i = 0; $i <= ($lol + 1) ; $i++){
        // store the start date in tmp variable.
        $tmpStart = $start;
        // get the day counter 0 for monday .. 6 for sunday
        $day    = date('N', $start);
    
        $times_table[$i]['StartDate']=$date;
        $times_table[$i]['Version']="v10";
    
        // check for sat and sun, if found increment to next monday
        if($day == 6 || $day == 7)
            $start = strtotime(' +'.(8 - $day).' day', $start);
    
        $times_table[$i]['DtMy']= "<strong>" . date("d-M", $start) . "</strong>";
    
        // restore the original variable
        $start = $tmpStart;
    
        // Increment one month
        $start = strtotime('+1 month', $start);
    }
    
    print_r($times_table);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题