所以我有一系列日期,对应于文章标记为“已读”/完成的日期。
我想要达到的目标是找到每天,每周和每月的平均阅读率。
理想情况下,我想要的是“平均每天阅读X篇文章”。
这段代码是我构建数组的地方:
$timeRead = $key["time_read"];
$dateRead = date('m/d/Y', $timeRead);
array_push($readDatesArray, $dateRead);
我知道我需要遍历数组,然后需要按日期分组,然后以某种方式获得平均值。非常感谢一些帮助!
foreach ($readDatesArray as $key) {
echo $key . "<br>";
}
数组:
Array
(
[0] => 07/14/2014
[1] => 07/13/2014
[2] => 07/12/2014
[3] => 07/12/2014
[4] => 07/12/2014
[5] => 07/12/2014
[6] => 07/12/2014
[7] => 07/11/2014
[8] => 07/11/2014
[9] => 07/11/2014
[10] => 07/10/2014
[11] => 07/09/2014
[12] => 07/11/2014
[13] => 07/09/2014
[14] => 07/09/2014
[15] => 07/09/2014
[16] => 07/09/2014
[17] => 07/09/2014
[18] => 07/05/2014
[19] => 07/04/2014
[20] => 07/05/2014
[21] => 07/05/2014
[22] => 07/05/2014
[23] => 07/04/2014
[24] => 07/02/2014
[25] => 07/02/2014
[26] => 07/02/2014
[27] => 07/04/2014
[28] => 07/02/2014
[29] => 07/02/2014
[30] => 06/29/2014
[31] => 06/29/2014
[32] => 06/29/2014
[33] => 06/29/2014
[34] => 06/24/2014
[35] => 06/29/2014
[36] => 06/20/2014
[37] => 06/20/2014
[38] => 06/14/2014
[39] => 06/07/2014
[40] => 06/07/2014
[41] => 06/05/2014
[42] => 06/05/2014
[43] => 06/05/2014
[44] => 06/03/2014
[45] => 06/07/2014
[46] => 06/05/2014
[47] => 05/31/2014
[48] => 05/31/2014
[49] => 06/05/2014
[50] => 05/28/2014
[51] => 05/20/2014
[52] => 07/09/2014
[53] => 05/10/2014
[54] => 05/10/2014
[55] => 05/10/2014
[56] => 05/10/2014
[57] => 05/03/2014
[58] => 05/03/2014
[59] => 05/03/2014
[60] => 04/24/2014
[61] => 04/20/2014
[62] => 05/28/2014
[63] => 04/20/2014
[64] => 04/20/2014
[65] => 04/20/2014
[66] => 04/18/2014
[67] => 04/18/2014
[68] => 04/12/2014
[69] => 04/20/2014
[70] => 04/02/2014
[71] => 04/20/2014
[72] => 03/27/2014
[73] => 04/02/2014
[74] => 04/02/2014
[75] => 03/27/2014
[76] => 03/16/2014
[77] => 03/16/2014
[78] => 03/16/2014
[79] => 03/16/2014
[80] => 03/16/2014
[81] => 03/02/2014
[82] => 03/02/2014
[83] => 03/02/2014
[84] => 02/16/2014
[85] => 02/16/2014
[86] => 02/10/2014
[87] => 02/22/2014
[88] => 02/10/2014
[89] => 02/23/2014
[90] => 03/02/2014
[91] => 03/16/2014
[92] => 03/16/2014
[93] => 03/16/2014
[94] => 01/13/2014
[95] => 01/13/2014
[96] => 01/13/2014
[97] => 01/08/2014
[98] => 01/08/2014
[99] => 01/08/2014
[100] => 01/08/2014
[101] => 05/28/2014
[102] => 03/27/2014
[103] => 03/27/2014
[104] => 03/27/2014
[105] => 03/27/2014
[106] => 01/08/2014
[107] => 03/27/2014
[108] => 03/27/2014
[109] => 03/27/2014
[110] => 03/27/2014
[111] => 12/15/2013
[112] => 12/15/2013
[113] => 12/19/2013
[114] => 03/27/2014
[115] => 03/27/2014
[116] => 12/15/2013
[117] => 12/15/2013
[118] => 12/15/2013
[119] => 12/15/2013
[120] => 03/27/2014
[121] => 03/27/2014
[122] => 12/15/2013
[123] => 12/15/2013
[124] => 12/15/2013
[125] => 03/27/2014
[126] => 12/15/2013
[127] => 12/15/2013
[128] => 03/27/2014
[129] => 12/15/2013
[130] => 12/15/2013
[131] => 03/27/2014
[132] => 03/27/2014
[133] => 12/15/2013
[134] => 03/16/2014
[135] => 12/05/2013
[136] => 12/15/2013
[137] => 03/27/2014
[138] => 03/27/2014
[139] => 12/15/2013
[140] => 03/27/2014
[141] => 12/15/2013
[142] => 12/15/2013
[143] => 03/27/2014
[144] => 12/03/2013
[145] => 12/03/2013
[146] => 12/03/2013
[147] => 12/03/2013
[148] => 12/03/2013
[149] => 12/03/2013
[150] => 12/01/2013
[151] => 12/01/2013
[152] => 12/03/2013
[153] => 12/03/2013
[154] => 12/03/2013
[155] => 12/03/2013
[156] => 12/03/2013
[157] => 11/30/2013
[158] => 11/30/2013
[159] => 12/03/2013
[160] => 11/30/2013
[161] => 12/03/2013
[162] => 12/03/2013
[163] => 12/03/2013
[164] => 03/27/2014
[165] => 11/30/2013
[166] => 11/30/2013
[167] => 12/03/2013
[168] => 12/03/2013
[169] => 12/03/2013
[170] => 11/30/2013
[171] => 11/30/2013
[172] => 11/30/2013
[173] => 03/27/2014
[174] => 03/27/2014
[175] => 11/30/2013
[176] => 11/22/2013
[177] => 11/30/2013
[178] => 11/30/2013
[179] => 11/30/2013
[180] => 11/30/2013
[181] => 11/30/2013
[182] => 12/03/2013
[183] => 11/30/2013
[184] => 11/30/2013
[185] => 11/16/2013
[186] => 11/16/2013
[187] => 11/15/2013
[188] => 11/15/2013
[189] => 11/15/2013
[190] => 11/15/2013
[191] => 11/15/2013
[192] => 11/15/2013
[193] => 05/28/2014
[194] => 11/15/2013
[195] => 11/15/2013
[196] => 11/09/2013
[197] => 11/09/2013
[198] => 11/15/2013
[199] => 11/07/2013
[200] => 04/18/2014
[201] => 11/23/2013
[202] => 11/03/2013
[203] => 11/03/2013
[204] => 11/01/2013
[205] => 10/27/2013
[206] => 10/27/2013
[207] => 10/29/2013
[208] => 10/27/2013
[209] => 10/27/2013
[210] => 10/27/2013
[211] => 05/28/2014
[212] => 10/29/2013
[213] => 10/25/2013
[214] => 10/29/2013
[215] => 10/29/2013
[216] => 10/29/2013
[217] => 10/29/2013
[218] => 10/25/2013
[219] => 12/03/2013
[220] => 10/20/2013
[221] => 10/20/2013
[222] => 10/20/2013
[223] => 10/20/2013
[224] => 12/30/2013
[225] => 10/20/2013
[226] => 11/19/2013
[227] => 10/20/2013
[228] => 10/16/2013
[229] => 10/15/2013
[230] => 10/20/2013
[231] => 10/16/2013
[232] => 10/16/2013
[233] => 10/11/2013
[234] => 10/11/2013
[235] => 10/11/2013
[236] => 10/10/2013
[237] => 10/09/2013
[238] => 12/15/2013
[239] => 10/08/2013
[240] => 10/08/2013
[241] => 10/07/2013
[242] => 10/06/2013
[243] => 10/06/2013
[244] => 11/03/2013
[245] => 10/06/2013
[246] => 10/06/2013
[247] => 10/06/2013
[248] => 10/06/2013
[249] => 10/07/2013
[250] => 10/04/2013
[251] => 10/02/2013
[252] => 10/04/2013
[253] => 03/16/2014
[254] => 11/03/2013
[255] => 10/06/2013
[256] => 11/03/2013
[257] => 09/30/2013
[258] => 09/30/2013
[259] => 10/06/2013
[260] => 09/29/2013
[261] => 09/29/2013
[262] => 09/29/2013
[263] => 09/30/2013
[264] => 09/29/2013
[265] => 04/02/2014
[266] => 09/30/2013
[267] => 09/28/2013
[268] => 09/28/2013
[269] => 09/28/2013
[270] => 09/28/2013
[271] => 09/25/2013
[272] => 09/25/2013
[273] => 09/25/2013
[274] => 09/25/2013
[275] => 09/25/2013
[276] => 09/25/2013
[277] => 09/25/2013
[278] => 09/25/2013
[279] => 09/25/2013
[280] => 09/25/2013
[281] => 09/25/2013
[282] => 03/17/2013
[283] => 03/17/2013
[284] => 02/18/2013
[285] => 09/25/2013
[286] => 09/25/2013
[287] => 09/25/2013
[288] => 02/01/2013
[289] => 02/01/2013
[290] => 02/01/2013
[291] => 02/01/2013
[292] => 01/27/2013
[293] => 01/27/2013
[294] => 09/25/2013
[295] => 09/25/2013
[296] => 09/25/2013
[297] => 02/01/2013
[298] => 09/25/2013
[299] => 09/25/2013
[300] => 09/25/2013
[301] => 09/25/2013
[302] => 09/25/2013
[303] => 09/25/2013
[304] => 09/25/2013
[305] => 09/25/2013
[306] => 09/25/2013
[307] => 09/25/2013
[308] => 09/25/2013
[309] => 09/25/2013
[310] => 09/25/2013
[311] => 09/25/2013
[312] => 01/20/2013
[313] => 01/20/2013
[314] => 01/19/2013
[315] => 01/19/2013
[316] => 09/25/2013
[317] => 09/25/2013
[318] => 01/20/2013
[319] => 01/20/2013
[320] => 09/25/2013
[321] => 09/25/2013
[322] => 09/25/2013
[323] => 09/25/2013
[324] => 01/17/2013
[325] => 09/25/2013
[326] => 09/25/2013
[327] => 01/17/2013
[328] => 09/25/2013
[329] => 09/25/2013
[330] => 01/15/2013
[331] => 01/15/2013
[332] => 01/15/2013
[333] => 09/25/2013
[334] => 09/25/2013
[335] => 09/25/2013
[336] => 01/15/2013
[337] => 01/15/2013
[338] => 01/11/2013
[339] => 01/11/2013
[340] => 01/10/2013
[341] => 01/18/2013
[342] => 01/11/2013
)
答案 0 :(得分:1)
技术上应该是这个
$datetime1 = new DateTime('2009-10-11'); // Whenever the user joined
$datetime2 = new DateTime(); // Now
$interval = $datetime1->diff($datetime2);
$number = (count($readDatesArray)/$interval->days);
echo "On average you read $number articles per day";
$number = (count($readDatesArray)/$interval->weeks);
echo "On average you read $number articles per week";
$number = (count($readDatesArray)/$interval->months);
echo "On average you read $number articles per month";
因为有一天他们可能会读0本书。因此数组数据日期是无关紧要的。重要的是他们实际阅读了多少本书。