M
M
My joy2016-10-09 22:55:19
PHP
My joy, 2016-10-09 22:55:19

Highcharts display visitors for the last month?

Good evening comrade friends.
There is a need to display site visitors for the last month (today -1 month)
The problem is that I did not have visitors on the site every day, and the query skips days when they were not. And you need to show 0 if that day was empty. Or you can somehow implement it on HC so that empty values ​​\u200b\u200bare substituted.
Here is the SQL

select left(date_visited, 10) as `date`, count(id) as `count` from visitors 
where date_visited between '$dateStart' and '$dateEnd'
group by left(date_visited, 10)

How to finish it for my needs, or how to file HC? Thanks for the hints

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
My joy, 2016-10-09
@t-alexashka

I came up with this solution, but maybe someone knows more elegant and correct

$period = new DatePeriod(
     new DateTime(date('Y-m-d', strtotime('now -30 days'))),
     new DateInterval('P1D'),
     new DateTime(date('Y-m-d'))
);

$visitorStats = [];

foreach($period as $date) { 
  $visitorStats[$date->format('Y-m-d')] = 0; 
}
// sql query ...
foreach($dbRows as $row) {
  $visitorStats[$row->date] = $row->count;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question