PHP:获取此代码的帮助,以获取一个月中一周的所有日期

时间:2014-03-10 06:08:17

标签: php date

我的代码需要在一个月的第一天和最后一天得到几周,我的代码正在运行,但在几个月内,上周的第一天和最后一天成为下个月的第一天和本月的最后一天。星期几是星期日。

输入Y-m-d format e.g.(2014-01-05)中的任何日期,当天无关紧要,因为它将被转换为该月的第一天

当月份是1月份有问题时,这是输出

2014-01-01 2014-01-04
2014-01-05 2014-01-11
2014-01-12 2014-01-18
2014-01-19 2014-01-25
2014-01-26 2014-01-31
2014-02-02 2014-01-31 // here lies the problem

当月份是正确时,这是输出

2014-02-01 2014-02-01
2014-02-02 2014-02-08
2014-02-09 2014-02-15
2014-02-16 2014-02-22
2014-02-23 2014-02-28

我正在使用的功能:

function getWeeks($from) {
        $array = array();

        $from = date("Y-m-d", strtotime($from));

        $start_date = date('Y-m-01', strtotime($from));
        $end_date = date('Y-m-t', strtotime($from));
        $end_date1 = date('Y-m-d', strtotime($end_date." + 6 days"));
        $week_array = array();
        for($date = $start_date; $date < $end_date1; $date = date('Y-m-d', strtotime($date. ' + 7 days')))
        {
            $getarray = getWeekDates($date, $start_date, $end_date);
            $week_array[] = $getarray;
        }

        return $week_array;
    }

function getWeekDates($date, $start_date, $end_date)
    {
        $week =  date('W', strtotime($date));
        $year =  date('Y', strtotime($date));
        $from = date("Y-m-d", strtotime("{$year}-W{$week}+3"));

        if($from < $start_date) $from = $start_date;

        $to = date("Y-m-d", strtotime("{$year}-W{$week}-6"));
        if($to > $end_date) $to = $end_date;

        $obj = new stdClass();
        $obj->from = $from;
        $obj->to   = $to;

        return $obj;
    }

我查看了Get all Work Days in a Week for a given date,但它无法回答我的问题,因为它使用了不同的实现。我想用我自己的实现。

2 个答案:

答案 0 :(得分:1)

我已经将一个可能有用的脚本混合在一起,你需要把它放到像以前一样的类中,或者它可能有助于找到代码的问题。无论哪种方式,希望它有所帮助。

PS我讨厌使用日期! :)

        $dateToday = date('Y-m');
        $monthToday = date('m');
        $yearToday = date('y');

        $totalDaysInMonth = cal_days_in_month(CAL_GREGORIAN, $monthToday, $yearToday);
        $dateOfFirstSunday = date('l Y-m-d', strtotime($dateToday . " first Sunday"));
        $dayOfFirstSunday = date('j', strtotime($dateToday . " first Sunday"));
        for($n = 1; $n < $dayOfFirstSunday; $n++){
            echo date('l Y-m-d', strtotime($dateToday . '-' . $n)) . "<br>";
        }

        echo $dateOfFirstSunday . "<br>";

        for($n = $dayOfFirstSunday + 7; $n <= $totalDaysInMonth; $n +=7){
            $lastN = $n;
            echo date('l Y-m-d', strtotime($yearToday . '-' . $monthToday . '-' . $n)) . "<br>";   
        }
        for($n = $lastN+1; $n <= $totalDaysInMonth; $n++){
            echo date('l Y-m-d', strtotime($dateToday . '-' . $n)) . "<br>";
        }

答案 1 :(得分:1)

在getWeeks函数上:

for($date = $start_date; $date < $end_date1; $date = date('Y-m-d', strtotime($date. ' + 7 days')))
{
    $getarray = getWeekDates($date, $start_date, $end_date);
    if($getarray) $week_array[] = $getarray; // change it to this
}
在getWeekDates函数上:

$from = date("Y-m-d", strtotime("{$year}-W{$week}+3"));

if($from > $end-date) return null; // Add this
if($from < $start_date) $from = $start_date;

$to = date("Y-m-d", strtotime("{$year}-W{$week}-6"));