用PHP

时间:2016-07-25 00:12:54

标签: php

我正在尝试计算自特定日期以来而不是从年初开始的月数和周数。

它不应该遵循日历月,而应该每四周计算一个月,并从指定日期开始。我需要能够显示月数,以及它的周数(1,2,3或4)。

我想输入一个开始日期,然后让它计算从该开始日期开始的月份和周数,例如,如果开始日期设置为8月1日,它应该显示第1周,第1周等等。

我的代码如下。我用一些不同的开始日期测试了它。以下列出了以下代码生成的代码以及 应该 显示的内容

Jun-20:应该是第2周 - 显示为第0周

Jun-27:应该是第1周 - 显示为第3周

04年7月:应该是第4周 - 显示为第2周

7月11日:应该是第3周 - 显示为第1周

7月18日:应该是第2周 - 显示为第0周

$monthNumber          = 5;
$monthStartDate       = '2016-06-13';
$currentStartWeekDate = date('l') != 'Monday' ? date("Y-m-d", strtotime("last monday")) : date("Y-m-d"); // get the current week's Monday's date

$weekDateCounter      = $monthStartDate;
$currentWeekNumber    = 0;

while ($weekDateCounter != $currentStartWeekDate){
  $currentWeekNumber += 1;
  $weekDateCounter    = date("Y-m-d", strtotime($weekDateCounter . "+7 days"));
  
  //
  if ($currentWeekNumber == 4){
    $currentWeekNumber  = 0; // reset week number
    $monthNumber       += 1; // increment month number
  }
}

我真的很茫然,可以使用任何帮助!

3 个答案:

答案 0 :(得分:0)

DateTime类可以让您更加简单。他们的文档在这里:http://php.net/manual/en/book.datetime.php

试试这个:

$date1 = new DateTime('2016-04-01');
$date2 = new DateTime('2016-07-24');

$diff = $date1->diff($date2);

$daysInbetween = $diff->days;
$weeksInbetween = floor($diff->days / 7);
$monthsInbetween = floor($weeksInbetween / 4);

print "Days inbetween = $daysInbetween" . PHP_EOL;
print "Weeks inbetween = $weeksInbetween" . PHP_EOL;
print "Months inbetween = $monthsInbetween" . PHP_EOL;

print "Total difference = $monthsInbetween months and "
    . ($weeksInbetween - ($monthsInbetween * 4)) . " weeks" . PHP_EOL;

答案 1 :(得分:0)

你的方法看起来过于复杂:

function weekCounter($startDate,$endDate=null){

    //use today as endDate if no date was supplied
    $endDate = $endDate? : date('Y-m-d');

    //calculate # of full weeks between dates
    $secsPerWeek = 60 * 60 * 24 * 7;
    $fullWeeks = 
            floor((strtotime($endDate) - strtotime($startDate))/$secsPerWeek);

    $fullMonths = floor($fullWeeks/4);
    $weeksRemainder = $fullWeeks % 4; // weeks that don't fit in a month

    //increment from 0-base to 1-base, so first week is Week 1. Same with months
    $fullMonths++; $weeksRemainder++;

    //return months and weeks in an array
    return [$fullMonths,$weeksRemainder];
}

您可以通过这种方式调用此函数,并捕获数月和数周:

//list() will assign the array members from weekCounter to the vars in list
list($months,$weeks) = weekCounter('2016-06-07'); //no end date, so today is used

//now $months and $weeks can be used as you wish
echo "Month: $months, Week: $weeks"; //outputs Month: 2, Week: 2

guide

答案 2 :(得分:0)

<?php
    /**
     * AUTHOR : VEDAVITH RAVULA
     * DATE : 13122019
     */
     function get_weeks($startDate = NULL,$endDate = NULL)
    {
            if(is_null($startDate) && is_null($endDate))
            {

                $start_date = date('Y-m-01');
                $end_Date = date('Y-m-t');

                $date1 = new DateTime($start_date);
                $date2 = new DateTime($end_Date);

                $interval = $date1->diff($date2);
                $weeks = floor(($interval->days) / 7);

                if(($date1->format("N") > 1) && ($date1->format("D") != "Sun"))
                {
                   $diffrence = "-".( $date1->format("N"))." Days";
                   $date1 = $date1->modify($diffrence);
                }
                    for($i = 0; $i <= $weeks; $i++)
                    {   
                        if($i == 0)
                        {
                            $start_date = $date1->format('Y-m-d');
                            $date1->add(new DateInterval('P6D'));

                        }
                        else
                        {
                            $date1->add(new DateInterval('P6D'));
                        }
                        echo $start_date." - ".$date1->format('Y-m-d')."\n";
                        $date1->add(new DateInterval('P1D'));
                        $start_date = $date1->format('Y-m-d');
                    }

            }
    }

    //function call
get_weeks();