如何从每周删除最后两天?

时间:2016-01-12 19:24:51

标签: php

我有一个日历(我在谷歌上找到)并对其进行了修改。但是,原始日历一周有7天(周一至周日)我只需要周一至周五 日历是使用<table><tr><td>创建的。

我尝试了很多东西,包括仅创建5 <td>。但是,如果我只创建5 <td>,那么第6个将是第5个之后的下一个数字,例如;星期五是第1天,所以星期一是第2天,但星期一应该是第4天。

以下是代码:

 class Calendar {  

/**
 * Constructor
 */
public function __construct(){     
    $this->naviHref = htmlentities($_SERVER['PHP_SELF']);
}

/********************* PROPERTY ********************/  
private $dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");

private $currentYear=0;

private $currentMonth=0;

private $currentDay=0;

private $currentDate=null;

private $daysInMonth=0;

private $naviHref= null;

/********************* PUBLIC **********************/  

/**
* print out the calendar
*/
public function show() {



    $year  = null;

    $month = null;

    if(null==$year&&isset($_GET['year'])){

        $year = $_GET['year'];

    }else if(null==$year){

        $year = date("Y",time());  

    }          

    if(null==$month&&isset($_GET['month'])){

        $month = $_GET['month'];

    }else if(null==$month){

        $month = date("m",time());

    }                  

    $this->currentYear=$year;

    $this->currentMonth=$month;

    $this->daysInMonth=$this->_daysInMonth($month,$year);  

    $content='<fieldset>'. 
             '<legend class="cyan bold">Tutors Schedule</legend>'.

                    //head nav '<' & '>' buttons & month 
                    $this->_createNavi().
                    //end head


                    '<table class="table table-bordered">'.

                        '<thead>'.
                             '<tr>'.$this->_createLabels().'</tr>' .
                        '</thead>' .

                        '<tbody>'.




                          '<tr>';


                            $weeksInMonth = $this->_weeksInMonth($month,$year);
                            // Create weeks in a month

                            for( $i=0; $i<$weeksInMonth; $i++ ){
                                //$content .= "<tr>";

                                    //Create days in a week
                                    for($j=1;$j<=7;$j++){

                                         $content.=$this->_showDay($i*7+$j);

      //                                           
                                    } 

                                  $content .="</tr>";  
                            }



                         '</tbody>'.
                    '</table>';

   '</fieldset>';
    return $content;   
}

/********************* PRIVATE **********************/ 
/**
* create the td element for tr
*/
private function _showDay($cellNumber){

    if($this->currentDay==0){

        $firstDayOfTheWeek = date('N',strtotime($this->currentYear.'-'.$this->currentMonth.'-01'));

        if(intval($cellNumber) == intval($firstDayOfTheWeek)){

            $this->currentDay=1;

        }
    }

    if( ($this->currentDay!=0)&&($this->currentDay<=$this->daysInMonth) ){

        $this->currentDate = date('m-d-Y',strtotime($this->currentYear.'-'.$this->currentMonth.'-'.($this->currentDay)));

        $cellContent = $this->currentDay;

        $this->currentDay++;   

    }else{

        $this->currentDate =null;

        $cellContent=null;
    }

    return '<td id="'.$this->currentDate.'" class=" td-top-text '.($cellNumber%7==1 ?' start ':($cellNumber%7==0 ?' end ':' ')).
            ($cellContent==null?'mask':'').'"><div class="inside">'.$cellContent.'</div></td>';

   }


   /**
* calculate number of weeks in a particular month
*/
private function _weeksInMonth($month=null,$year=null){

    if( null==($year) ) {
        $year =  date("Y",time()); 
    }

    if(null==($month)) {
        $month = date("m",time());
    }

    // find number of days in this month
    $daysInMonths = $this->_daysInMonth($month,$year);

    $numOfweeks = ($daysInMonths%7==0?0:1) + intval($daysInMonths/7);

    $monthEndingDay= date('N',strtotime($year.'-'.$month.'-'.$daysInMonths));

    $monthStartDay = date('N',strtotime($year.'-'.$month.'-01'));

    if($monthEndingDay<$monthStartDay){

        $numOfweeks++;

    }

    return $numOfweeks;
}

/**
* calculate number of days in a particular month
*/
private function _daysInMonth($month=null,$year=null){

    if(null==($year))
        $year =  date("Y",time()); 

    if(null==($month))
        $month = date("m",time());

       return date('t',strtotime($year.'-'.$month.'-01'));
    }


 }

这就是日历的样子:

enter image description here

我想做什么; enter image description here

测试:

Application.php

$calendar = new Calendar();
echo $calendar->show();

谢谢!

修改

如果我改变了这一点:

private $dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
//Create days in a week
     for($j=1;$j<=7;$j++){

       $content.=$this->_showDay($i*7+$j);

对此:

 private $dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday");
//Create days in a week
     for($j=1;$j<=5;$j++){

       $content.=$this->_showDay($i*5+$j);

我会得到这个:

enter image description here

2 个答案:

答案 0 :(得分:1)

改变这个:

private $dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");

for($j=1;$j<=7;$j++){

      $content.=$this->_showDay($i*7+$j);

 } 

//show day return statement
    return '<td id="'.$this->currentDate.'" class=" td-top-text '.($cellNumber%7==1 ?' start ':($cellNumber%7==0 ?' end ':' ')).
            ($cellContent==null?'mask':'').'"><div class="inside">'.$cellContent.'</div></td>';

对此:

private $dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday");
for($j=1;$j<=5;$j++){

      $content.=$this->_showDay($i*5+$j);

 } 

return '<td id="'.$this->currentDate.'" class=" td-top-text '.($cellNumber%5==1 ?' start ':($cellNumber%5==0 ?' end ':' ')).
        ($cellContent==null?'mask':'').'"><div class="inside">'.$cellContent.'</div></td>';

答案 1 :(得分:1)

选择最简单的路线,使用css:)

.calendar td:nth-child(n+6) {
    display: none;
}

我们假设您的日历的css类为calendar