Aggregating time-series data with PHP