dousa1630 2018-07-30 02:02
浏览 9
已采纳

在不添加周末的情况下获得数月的日子

with this code i can get the days in the whole month, now how to remove saturday and sunday?

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条回答 默认 最新

  • dongshu9458 2018-07-30 02:22
    关注

    Explanation inline & in comments!

    /*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 )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接