每月获得几天而无需增加周末

时间:2018-07-30 02:02:53

标签: php laravel

使用此代码,我可以获得整个月的日子,现在如何删除周六和周日?

public function days(){
$days=array();

$month = 3;
$year = 2018;

for($d=1; $d<=31; $d++)
{
    $time=mktime(12, 0, 0, $month, $d, $year);          
    if (date('m', $time)==$month)       
        $days[]=date('Y-m-d H:i:s', $time);



}

return view('days',compact('days'))}

2 个答案:

答案 0 :(得分:2)

喜欢在这里get name of day

$days=array();
$month = 3;
$year = 2018;
$firstdate = $year.'-'.$month.'-01';  //taked firstdate using month and year
$lastdate = date('t', strtotime($firstdate)); //get last date from first date of month
for($d=1; $d<=$lastdate; $d++) // loop till last date
{
    $time=mktime(12, 0, 0, $month, $d, $year);   
    //check if day not equal Sat on Sun date('D', $time) != "Sat" or (date('D', $time) != "Sun" )      
    if ((date('m', $time)==$month && ($dayis != 'Sat')) && (date('m', $time)==$month && ($dayis != 'Sun'))){  
        echo date('D', $time)."<br>"; //to echo day name
        $days[]=date('Y-m-d H:i:s', $time); // store dates in array if not Saturday or Sunday
    }
}

var_dump($days); // print all stored date(var_dump is just to check data)

答案 1 :(得分:1)

内联和注释说明!     

/*public*/ function days($year, $month){
   $days=array();

   for($d=1; $d<=31; $d++) {
       $time=mktime(12, 0, 0, $month, $d, $year);  
       // here check if weekday (date('w', $time)) is greater than sun (=0) and lower than sat (=6)        
       if(date('m', $time)==$month && date('w', $time)>0 && date('w', $time)<6) {
         $days[]=date('Y-m-d H:i:s', $time);
       }
   }
   return $days;
}

print_r(days(2018, 3));
// output: 
// Array ( [0] => 2018-03-01 12:00:00 [1] => 2018-03-02 12:00:00 [2] => 2018-03-05 12:00:00 [3] => 2018-03-06 12:00:00 [4] => 2018-03-07 12:00:00 [5] => 2018-03-08 12:00:00 [6] => 2018-03-09 12:00:00 [7] => 2018-03-12 12:00:00 [8] => 2018-03-13 12:00:00 [9] => 2018-03-14 12:00:00 [10] => 2018-03-15 12:00:00 [11] => 2018-03-16 12:00:00 [12] => 2018-03-19 12:00:00 [13] => 2018-03-20 12:00:00 [14] => 2018-03-21 12:00:00 [15] => 2018-03-22 12:00:00 [16] => 2018-03-23 12:00:00 [17] => 2018-03-26 12:00:00 [18] => 2018-03-27 12:00:00 [19] => 2018-03-28 12:00:00 [20] => 2018-03-29 12:00:00 [21] => 2018-03-30 12:00:00 )