簡體   English   中英

如何獲得給定月份中每周的日期間隔?

[英]How to get the date interval for each week in a given month?

有沒有辦法在一個月的PHP中獲取每周的日期開始/結束? 所以,例如,如果我這個月(2011年11月),我需要返回

30 Oct ->  5 Nov
 6 Nov -> 12 Nov
13 Nov -> 19 Nov
20 Nov -> 26 Nov
27 Nov ->  3 Dec

LE:

這是我所做的,以防其他人需要它。 代碼不漂亮但它可以正常工作。

public function getMonthWeeks($month, $year)
{
    $month = intval($month);

    if ($month < 1) {
        $month = 1;
    }

    if ($month > 12) {
        $month = 12;
    }

    $tsfdOfMonth = mktime(0, 0, 0, $month, 1, $year);
    $dayOfWeek   = idate('w', $tsfdOfMonth);

    $tIntervalStart = $tsfdOfMonth;
    $tNextMonth     = idate('m', strtotime("+1 month", $tsfdOfMonth));

    if ($dayOfWeek > 0) {
        $tStr = sprintf("-%d %s",
            $dayOfWeek,
            $dayOfWeek == 1 ? 'day' : 'days'
        );
        $tIntervalStart = strtotime($tStr, $tsfdOfMonth);
    }

    $resultDates = array();

    $tsStart = $tIntervalStart;
    $tsEnd   = strtotime('+6 days', $tsStart);

    while (true) {
        $rObj = new stdClass;
        $rObj->LinkStr = sprintf("%s %s - %s %s",
            date('M', $tsStart), date('d', $tsStart),
            date('M', $tsEnd),   date('d', $tsEnd)
        );
        $rObj->DateStart = date('Y-m-d', $tsStart);
        $rObj->DateEnd   = date('Y-m-d', $tsEnd);

        $resultDates[] = $rObj;

        if (idate('m', strtotime('+1 day',  $tsEnd)) == $tNextMonth) {
            break;
        }

        $tsStart = strtotime('+1 day',  $tsEnd);
        $tsEnd   = strtotime('+6 days', $tsStart);
    }

    return $resultDates;
}

你必須寫一個函數:

算法:

  1. 獲取每月第一周的某一天。
  2. 減去到達星期日所需的天數 - 您現在擁有該月的第一個“星期日”的日期(可能是上個月的上個星期日)。
  3. 添加六天以確定該周的星期六。
  4. 添加一天以確定下周的星期日。
  5. 重復3和4,直到我們不再在當月

兩個功能將非常有用: $saturdaytimestamp = strtotime("+6 days", $sundaytimestamp)$dayofweek = idate('w', $timestamp); $saturdaytimestamp = strtotime("+6 days", $sundaytimestamp) $dayofweek = idate('w', $timestamp);

你應該使用這些函數 - cal_days_in_month,strtotime,date

$aMonthWeeks = get_month_week_day_ranges(2011, 11);
print_r( $aMonthWeeks );

function get_month_week_day_ranges ( $year, $month ) {
    $last_month_day_num = cal_days_in_month(CAL_GREGORIAN, $month, $year); 

    $first_month_day_timestamp = strtotime($year.'-'.$month.'-01');
    $last_month_daty_timestamp = strtotime($year.'-'.$month.'-'.$last_month_day_num );

    $first_month_week = date('W', $first_month_day_timestamp);
    $last_month_week = date('W', $last_month_daty_timestamp);

    $aMonthWeeks = array();
    for( $week = $first_month_week; $week <= $last_month_week; $week ++ ) {
        echo sprintf('%dW%02d-1', $year, $week ), "\n";
        array_push( $aMonthWeeks, array(  
            date("d:m:Y", strtotime( sprintf('%dW%02d-1', $year, $week ) ) ),
            date("d:m:Y", strtotime( sprintf('%dW%02d-7', $year, $week ) ) ),
        ) );
    }
    return $aMonthWeeks;
}

對於我的語言環境(一周的第一天是星期一 ),結果是

Array
(
    [0] => Array
        (
            [0] => 31:10:2011
            [1] => 06:11:2011
        )

    [1] => Array
        (
            [0] => 07:11:2011
            [1] => 13:11:2011
        )

    [2] => Array
        (
            [0] => 14:11:2011
            [1] => 20:11:2011
        )

    [3] => Array
        (
            [0] => 21:11:2011
            [1] => 27:11:2011
        )

    [4] => Array
        (
            [0] => 28:11:2011
            [1] => 04:12:2011
        )

)

我不知道最好的方法是什么,但這可能有用:你問PHP哪一天是一個月的第一天,比如date('w', mktime(0, 0, 0, (int)date('m'), 1, (int)date('Y'));然后你可以倒數到一周的第一天,使用checkdate()函數來確定上個月的最后一天,然后用一個簡單的while()構建你的數組,直到你到達一個星期但你沒有在下個月開始。

我只是花時間為你編寫代碼,這將輸出你想要的:

<?php

/* CONFIG */

$year = 2011;

$month = 11;

/* CODE */

$first_day = mktime(0, 0, 0, $month, 1, $year);

$month_first_day = date('w', $first_day);

if($month_first_day != 1) {

    $offset = $month_first_day - 1;

    $previous_month = ($month == 1 ? mktime(0, 0, 0, 12, 1, ($year - 1)) : mktime(0, 0, 0, ($month - 1), 1, $year));

    $previous_month_days = date('t', $previous_month);

    $previous_month_saturday = $previous_month_days - $offset;

    $previous_month_stamp = ($month == 1 ? mktime(0, 0, 0, 12, $previous_month_saturday, ($year - 1)) : mktime(0, 0, 0, ($month - 1), $previous_month_saturday, $year));

    $first_saturday = $previous_month_stamp;

}else{

    $first_saturday = $first_day;

}

$loops = date('t', $first_day);

$loops = round($loops / 7);

for($loop = 1; $loop < ($loops + 2); $loop++) {

    echo date('j M', $first_saturday + ((7 * ($loop - 1)) * 24 * 60 * 60)) . ' -> ' . date('j M', $first_saturday + (((7 * $loop) - 1) * 24 * 60 * 60)) . '<br />';

}

這可能不是實現目標的最有效方式。 然而,它得到了你要求完成的東西,它讓你大致了解如何實現這一目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM