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吧。
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=[]); } //返回结果是一个按照第几周组成的复合数组。
getWeekOfDate方法来自 http://stackoverflow.com/questions/9045868/javascript-date-getweek
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 < 4) { weeknum = Math.floor((daynum+day-1)/7) + 1; if(weeknum > 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 < 4 ? 1 : 53; } } else { weeknum = Math.floor((daynum+day-1)/7); } return weeknum; };
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>

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor