douou6696
2016-07-25 00:12
浏览 417
已采纳

用PHP手动计算两个日期之间的周数

I'm trying to calculate the number of months and weeks since a particular date instead of from the beginning of the year.

It shouldn't follow calendar months but should instead count a month as every 4 weeks, and begin from a specified date. I need to be able to display the number of months, and also what week it is (1, 2, 3 or 4).

I want to put in a start date, and have it then count what month and week is it from that start date e.g if the start date is set to Mon 1st August it should show Month 1, Week 1 and so on.

My code is below. I tested it with some different start dates. Here's a list of what the code below generates and what it should display

Jun-20: Should be Week 2 - Shows as Week 0

Jun-27: Should be Week 1 - Shows as Week 3

Jul-04: Should be Week 4 - Shows as Week 2

Jul-11: Should be Week 3 - Shows as Week 1

Jul-18: Should be Week 2 - Shows as Week 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
  }
}

I am really at a loss with this and could use any help!

</div>

图片转代码服务由CSDN问答提供 功能建议

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

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

我想要开始日期,并且有 它然后计算从该开始日期开始的月份和周数,例如,如果开始日期设置为8月1日,它应该显示第1周,第1周,依此类推。

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

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

6月27日:应该是第1周 - 显示为第3周

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

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

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

\ r
  $ monthNumber = 5; 
 
 $ monthStartDate =  '2016-06-13'; 
 
 $ currentStartWeekDate = date('l')!='Monday'?  date(“Y-m-d”,strtotime(“last lastday”)):date(“Y-m-d”);  //获取当前周的星期一日期
 
 
 
 $ weekDateCounter = $ monthStartDate; 
 
 $ currentWeekNumber = 0; 
 
 
 
 \ while($ weekDateCounter!= $ currentStartWeekDate){
 
  
 $ currentWeekNumber + = 1; 
 
 $ weekDateCounter = date(“Ymd”,strtotime($ weekDateCounter。“+ 7天”)); 
 
 
 
 // 
 
 if(  $ currentWeekNumber == 4){
 
 $ currentWeekNumber = 0;  //重置周数
 
 $ monthNumber + = 1;  //增量月号
 
} 
 
}   
 
  
 
  
 
 
 
 

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

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douhe1864 2016-07-25 01:30
    已采纳

    Your approach seems overly complicated:

    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];
    }
    

    You can call the function this way, and capture months and weeks:

    //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
    

    Live demo

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题