douchengchen7959 2012-06-04 18:44
浏览 9
已采纳

得到没有星期五的月份的数字

I need to get the number of days in a month, but without for example Fridays.

$num = cal_days_in_month(CAL_GREGORIAN, $month, 2012);

Here I can get the total number of days, but I need to substract Fridays from the count. How do I do it?

  • 写回答

1条回答 默认 最新

  • dou44481 2012-06-04 19:51
    关注

    I found a handy function here:

    function num_days ($day, $month, $year) { 
        $day_array = array("Mon" => "Monday", "Tue" => "Tuesday", "Wed" => "Wednesday", "Thu" => "Thursday", "Fri" => "Friday", "Sat" => "Saturday", "Sun" => "Sunday");
    
        $month_array = array(1 => "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
    
        /* * Check our arguments are valid. */
    
        /* * $day must be either a full day string or the 3 letter abbreviation. */ 
        if (!(in_array($day, $day_array) || array_key_exists($day, $day_array))) { 
            return 0; 
        }
    
        /* * $month must be either a full month name or its 3 letter abrreviation */ 
        if (($mth = array_search(substr($month,0,3), $month_array)) <= 0) { 
            return 0; 
        }
    
        /* * Now fetch the previous $day of $month+1 in $year; * this will give us the last $day of $month. */
    
        /* * Calculate the timestamp of the 01/$mth+1/$year. */ 
    
        $time = mktime(0,0,0,$mth+1,1,$year);
        $str = strtotime("last $day", $time);
    
        /* * Return nth day of month. */ 
    
        $date = date("j", $str);
    
        /* * If the difference between $date1 and $date2 is 28 then * there are 5 occurences of $day in $month/$year, otherwise * there are just 4. */ 
    
        if ($date <= 28) { 
            return 4; 
        } else { 
            return 5; 
        } 
    } 
    

    Usage:

    echo date('t', strtotime("$month 2012")) - num_days('Friday', $month, 2012);

    Provided $month is a full name of a month (not a number).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端