Hi,小弟我现在有一个数据,是按照每天计算出来的tp.对应的数据为
<code>["2011-1-1","2011-1-2","2011-1-3","2011-1-4",...] </code>
每一天对应的数据(tp)
<code>[1,2,2,3,...] </code>
那么现在要讲这些日期按照周分类,没就是计算的结果为
<code>["2011年第一周",...] </code>
数据为
<code>[8,...] </code>
那么现在该怎么做呢?? 用什么语言实现都无所谓~~~~
Hi,小弟我现在有一个数据,是按照每天计算出来的tp.对应的数据为
<code>["2011-1-1","2011-1-2","2011-1-3","2011-1-4",...] </code>
每一天对应的数据(tp)
<code>[1,2,2,3,...] </code>
那么现在要讲这些日期按照周分类,没就是计算的结果为
<code>["2011年第一周",...] </code>
数据为
<code>[8,...] </code>
那么现在该怎么做呢?? 用什么语言实现都无所谓~~~~
<code><?php $date_list = null; $num_list = null; $index = 0; while($index < 20) { $date_list[] = date('Y-m-d',strtotime('-' . $index . ' day')); $num_list[] = $index; $index++; } // 先别管上面的代码,只是为了生成你的数据 $ret_list = null; // 假设日期数组和值数组的索引一致 foreach($date_list as $k => $date) { if($ret_list[date('W', strtotime($date))]) { $ret_list[date('W', strtotime($date))] += $num_list[$k]; } else { $ret_list[date('W', strtotime($date))] = $num_list[$k]; } } echo("日期数组:<br>"); print_r($date_list); echo("<br>"); echo("数值数组:<br>"); print_r($num_list); echo("<br>"); echo("按周统计数组(数组的键就是今年的第几周):<br>"); print_r($ret_list); </code>
重点在于date函数的使用,如何获取日期属于年度的第几周,看看php的doc吧。
<code class="javascript">var datas = ["2011-1-1","2011-1-2","2011-1-3","2011-1-4",...] function toWeekAarry(data){ var dateArr ,dateObj ,currWeek = 0; ,weeks = []; //假设楼主的数据不是连续的,而且不是全部元素合法 datas.filter(dateStr=>typeof dateStr==="string").forEach(dateStr=>{ dateArr = dateStr.split("-") if(dateArr.length===3){ dateObj = new Date(dateArr[0],dateArr[1],dateArr[2]) currWeek = getWeekOfDate(dateObj) if(!weeks[])weeks[currWeek]=[] weeks[currWeek].push(date); } }) retrun weeks.map(week=>if(!week)week=[]); } //返回结果是一个按照第几周组成的复合数组。</code>
getWeekOfDate方法来自 http://stackoverflow.com/questions/9045868/javascript-date-getweek
<code class="javascript">function getWeekOfDate (dateObj,dowOffset) { dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero var newYear = new Date(dateObj.getFullYear(),0,1); var day = newYear.getDay() - dowOffset; //the day of week the year begins on day = (day >= 0 ? day : day + 7); var daynum = Math.floor((dateObj.getTime() - newYear.getTime() - (dateObj.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1; var weeknum; //if the year starts before the middle of a week if(day 52) { nYear = new Date(dateObj.getFullYear() + 1,0,1); nday = nYear.getDay() - dowOffset; nday = nday >= 0 ? nday : nday + 7; /*if the next year starts before the middle of the week, it is week #1 of that year*/ weeknum = nday </code>
1)计算日期对应的周信息
<code>function getWeekOfYear(date){ var year=date.getFullYear(); var baseDate=new Date(year,0,1); var baseWeekDay=testDate.getDay(); var dateWeekDay=date.getDay(); if(baseWeekDay==0) baseWeekDay=7; if(dateWeekDay==0) dateWeekDay=7; var weekOfYear=Math.round(((date.getTime()-dateWeekDay*3600*24*1000)-(baseDate.getTime()-baseWeekDay*3600*24*1000))/(24*3600*1000)/7); return weekOfYear+1; }</code>
2)将在同一周的数据相加
<code> var dateArray=["2011-1-1","2011-1-2","2011-1-3","2014-10-01",'2014-01-01','2015-11-11']; var dateTpArray=[9,8,7,6,121323445,23492742742374324]; var dateReg=/^(\d{4})\-(\d{1,2})\-(\d{1,2})$/; var reduceResult={}; dateArray.forEach(function(item,index){ var matchResult=dateReg.exec(item); console.log('%s %s %s',matchResult[1],matchResult[2],matchResult[3]); var weekOfYear=getWeekOfYear(new Date(parseInt(matchResult[1],10),parseInt(matchResult[2],10)-1,parseInt(matchResult[3],10))); if(!reduceResult[matchResult[1]+'_'+weekOfYear]){ reduceResult[matchResult[1]+'_'+weekOfYear]={ desc:matchResult[1]+'第'+weekOfYear+'周', tps:0 } } reduceResult[matchResult[1]+'_'+weekOfYear].tps=reduceResult[matchResult[1]+'_'+weekOfYear].tps+dateTpArray[index]; }); var weekOfYearDescArray=[]; var weekofYearTpArray=[]; for(var prop in reduceResult){ if(reduceResult.hasOwnProperty((prop))){ weekOfYearDescArray.push(reduceResult[prop].desc); weekofYearTpArray.push(reduceResult[prop].tps); } } console.log(weekOfYearDescArray); console.log(weekofYearTpArray);</code>