使用PHP获取给定月份和年份的一个月中每周的总日期

时间:2015-10-08 10:08:37

标签: php arrays date multidimensional-array

怎么做?

例如,查看2015年10月的日历

-  -  -  1  2  3  4    <---- 1st Week - TOTAL : 4 Days
5  6  7  8  9  10 11   <---- 2nd Week - TOTAL : 7 Days
12 13 14 15 16 17 18   <---- 3rd Week - TOTAL : 7 Days
19 20 21 22 23 24 25   <---- 4th Week - TOTAL : 7 Days
26 27 28 29 30 31 -    <---- 5th Week - TOTAL : 6 Days

现在我想在一个数组中得到总共4天,7天,7天等,就像这样。

Array (
    [0] => Array
        ([TOTAL] => 4)
    [1] => Array
        ([TOTAL] => 7)
    [2] => Array
        ([TOTAL] => 7)
    [3] => Array
        ([TOTAL] => 7)
    [4] => Array
        ([TOTAL] => 6)
)

提前致谢。

3 个答案:

答案 0 :(得分:1)

<?php
$date = '1-Oct-2015';
$date_timstamp = strtotime($date);
$day_in_month = date('t', $date_timstamp);
$arr_day_in_week = array();
$j=0;
for($i=0; $i<$day_in_month; $i++){
    $day = date('D', $date_timstamp);
    if($day == 'Sun'){
        $j++;
        $arr_day_in_week[] = $j;
        $j=0;
    }else{
        $j++;
    }
    $date_timstamp += 24*60*60;
}

if($j>0){
    $arr_day_in_week[] = $j;
}

print_r($arr_day_in_week);

?>

答案 1 :(得分:0)

这篇文章可能很好用: https://codereview.stackexchange.com/questions/5451/is-this-a-good-algorithm-to-find-each-week-in-a-given-month

不确定它是否能完全解决您的问题,但这是一个很好的起点imo

答案 2 :(得分:0)

得到它......通过修改哈利答案

$_TOTAL_DAY = cal_days_in_month(CAL_GREGORIAN,'10','2015');
$_COLLECT = array();
$SUM_WEEK = 0;

for($I=1; $I <= $_TOTAL_DAY; $I++){

    $LOOP_DATE = date($year.'-'.$month.'-'.$I);
    $LOOP_DAY = strtoupper(date('D',strtotime($LOOP_DATE)));

    if($LOOP_DAY == 'SUN'){
        $SUM_WEEK++;
        $_COLLECT[] = $SUM_WEEK;
        $SUM_WEEK = 0;
    } else{
        $SUM_WEEK++;
    }

}

if($SUM_WEEK>0){
    $_COLLECT[] = $SUM_WEEK;
}

print_r($_COLLECT);
die();
谢谢你:)