<?php
  
                         
                        $user = 'stats'; $passwd = 'password'; 
                        $pdo = new PDO('mysql:host=localhost;dbname=statistics', $user, $passwd); 
                         
                        $day = mktime(0, 0, 0, date("m"), 1, date("Y")); 
                        $firstdate = date("Y", $day) . "-" . date("m", $day) . "-" . date("d", $day); 
                        $day = mktime(0, 0, 0, date("m") + 1, 1, date("Y")) - 1; 
                        $lastdate = date("Y", $day) . "-" . date("m", $day) . "-" . date("d", $day); 
                         
                        $sql =  "SELECT site_url AS _site, access_date AS _date, access_count AS _count FROM web_sites, site_hits "; 
                        $sql .= "WHERE web_site = site_id "; 
                        $sql .= "  AND access_date >= '$firstdate' AND access_date <= '$lastdate' "; 
                        $sql .= "ORDER BY site_seqnum, access_date"; 
                        $sth = $pdo->query($sql); 
                         
                        if ($sth->rowCount() > 0) { 
                            $days = date("j"); 
                            $oldsite = ''; $nsites = 0; 
                             
                            while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { 
                                 
                                if ($row['_site'] != $oldsite) { 
                                     
                                    $nsites++; 
                                    $totals[$nsites] = 0; $zeros[$nsites] = 0; $nonzeros[$nsites] = 0; $maximums[$nsites] = 0; 
                                    $averages[$nsites] = 0; $yesterdays[$nsites] = 0; $todays[$nsites] = 0; 
                                    $sites[$nsites] = $row['_site']; 
                                    $oldsite = $sites[$nsites]; 
                                } 
                                 
                                $dateparts = explode('-', $row['_date']); $day = $dateparts[2]; 
                                 
                                $totals[$nsites] += $row['_count']; 
                                $nonzeros[$nsites]++; 
                                if ($row['_count'] > $maximums[$nsites]) { 
                                    $maximums[$nsites] = $row['_count']; 
                                } 
                                if ($day == $days - 1) { 
                                    $yesterdays[$nsites] = $row['_count']; 
                                } 
                                else if ($day == $days) { 
                                    $todays[$nsites] = $row['_count']; 
                                } 
                            } 
                             
                            for ($i = 1; $i <= $nsites; $i++) { 
                                $zeros[$i] = $days - $nonzeros[$i]; 
                                $averages[$i] = sprintf("%01.2f", round($totals[$i] / $days, 2)); 
                                $differences[$i] = sprintf("%01.2f", abs($todays[$i] - $averages[$i])); 
                                if ($todays[$i] - $averages[$i] > 0) { 
                                    $differences[$i] = '+' . $differences[$i]; 
                                } 
                                else { 
                                    $differences[$i] = '–' . $differences[$i]; 
                                } 
                            } 
                             
                            echo '<table border="1" cellpadding="5">'; 
                            echo '<tr>'; 
                            echo '<th align="center">Site url</th><th align="center">Month<br/>days</th align="center"><th align="center">Zero<br/>days</th>'; 
                            echo '<th align="center">Total<br/>visits</th><th align="center">Max<br/>visits</th><th align="center">Average<br/>visits</th>'; 
                            echo '<th align="center">Yday<br/>visits</th><th align="center">Today<br/>visits</th><th align="center">Average<br/>difference</th>'; 
                            echo '</tr>'; 
                             
                            for ($i = 1; $i <= $nsites; $i++) { 
                                echo '<tr>'; 
                                echo '<td align="left">', $sites[$i], '<td align="right">', $days, '</td><td align="right">', $zeros[$i], '</td>'; 
                                echo '<td align="right">', $totals[$i], '</td><td align="right">', $maximums[$i], '</td><td align="right">', $averages[$i], '</td>'; 
                                echo '<td align="right">', $yesterdays[$i], '</td><td align="right">', $todays[$i], '</td><td align="right">', $differences[$i], '</td>'; 
                                echo '</tr>'; 
                            } 
                            echo '</table>'; 
                        } 
                         
                        else { 
                            echo '<p>There are no site hits for this month...</p>'; 
                        }
  
                        ?>
                        
                       | 
                     
                    |