Sponsor
Now Playing
- Alix Perez – I'm Free 3 hours ago
- Alix Perez – Intersections 3 hours ago
- Alix Perez – Forsaken 3 hours ago
- Glen E Ston – Ouroboros (Original Mix) 3 hours ago
- Black Sun Empire – Everything 3 hours ago
Feeling Dirty After So Much SQL
April 28, 2009,
19 views
What this does is go through my Apache log in my MySQL DB and calculate unique hits, overall hits, amount of bandwidth used per month, and only for the past 3 months! Making sure each month begins and ends on the first and last day of the month, not using timestamps from the current date! Phew. Brain hurts.
$size = $this->Show->Episode->Log->find('all', array('conditions'=>array('Log.filecode'=>$filecodes,'UNIX_TIMESTAMP(`timestamp`)
BETWEEN
UNIX_TIMESTAMP(DATE_FORMAT(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), \'%Y%m01000000\'))
AND
UNIX_TIMESTAMP(DATE_FORMAT(LAST_DAY(NOW()), \'%Y%m%d235959\'))'), 'group'=>array('YEAR(timestamp)', 'MONTH(timestamp)'),
'fields'=>array('Log.*', 'UNIX_TIMESTAMP(timestamp) as timestamp', 'SUM(Log.size) AS size', 'COUNT(*) as hits', 'COUNT(distinct ip) as uhits'), 'order'=>array('timestamp DESC')));