gpt4 book ai didi

php - 使用 SQL 和 php 创建网站使用情况的每周和每月图表

转载 作者:行者123 更新时间:2023-11-29 14:40:17 25 4
gpt4 key购买 nike

所以我正在为我正在为工作而构建的网站构建一个统计部分(经理要求)。我有一个“24 小时”图表的功能代码。我的图形代码工作得很好,但我在为 mysql 获取正确的代码来根据天而不是小时提取数据时遇到了麻烦

下面的每小时代码

function get_hourly_graph() {
// First lets create a daily graph for experiment then we can build on that
date_default_timezone_set('Australia/Sydney');
$current_hour = date('H');

// SQL Query to get data we want
$sql = mysql_query("SELECT hour( time_hit ) AS hour , count( hour( time_hit ) ) AS hits FROM hits WHERE time_hit > now( ) - INTERVAL 1 DAY GROUP BY hour( time_hit );")
or die(mysql_error());

// Remove an hour off current time for array and the loop that will put the data into the array
$array_hour = $current_hour - 1;

// Add a 0 if the timer is under 10. just so this is compatable with the SQL data
if ($array_hour < 10) {
$array_hour = 0 . $array_hour;
}

// initialise the array so we can set up the indexes and data in the next couple of loops
$hit_data = array();

// Work back the clock 24 hours and create the array indexes
for ($i = 0; $i < 24; $i++) {
$new_hour = $current_hour;

if ($new_hour < 1) {
$new_hour = 24;
$hit_data[$new_hour] = "0";
}
else {
$hit_data[$new_hour] = "0";
}

$current_hour = $new_hour - 1;
}

// Loop through the rest of the array and replace null with 0s
while ($results = mysql_fetch_array($sql, MYSQL_ASSOC)) {
foreach ($hit_data as $hour => $hits) {
if ($results['hour'] == $hour) {
$hit_data[$hour] = $results['hits'];
}
}
}

$hit_data = array_reverse($hit_data, true);

// Generate the graph for the 24 hour data
generate_graph ($hit_data, '24 Hour Report', 'hourly_graph.png', 'Time (Hours)', 'Hits');
}

“点击”表基本上由 3 个字段“hit_id”“ip_address”“timestamp”组成,位于“YYYY-MM-DD hh:mm:ss”=“2011-08-04 12:40:34”

如何更改下面的 SQL 代码以使用天而不是小时?

最佳答案

SELECT DATE_FORMAT('date_column', '%Y-%m-%e') as day ...... group by day

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

关于php - 使用 SQL 和 php 创建网站使用情况的每周和每月图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8129175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com