fork download
  1. <?php
  2.  
  3. class JsonDate{
  4.  
  5. public $start_time;
  6. public $end_time;
  7. public $duration = 2;
  8.  
  9. public function getDateCounts() {
  10.  
  11. $this->start_time = 1369706475;
  12. $this->end_time = 1369706492;
  13. $dates = array(
  14. 1369706475,
  15. 1369706477,
  16. 1369706478,
  17. 1369706479,
  18. 1369706481,
  19. 1369706486,
  20. 1369706486,
  21. 1369706487,
  22. 1369706489,
  23. 1369706492,
  24. );
  25.  
  26. $dateCounts = array();
  27. for(
  28. $to = $this->start_time + $this->duration;
  29. $to < $this->end_time;
  30. $to += $this->duration
  31. ) {
  32. $from = $to - $this->duration;
  33. if (!isset($dateCounts[$to]))
  34. $dateCounts[$to] = 0;
  35. foreach ($dates as $i => $date) {
  36. if ($from < $date) {
  37. if($date <= $to) {
  38. $dateCounts[$to]++;
  39. } else {
  40. break;
  41. }
  42. } else {
  43. unset($dates[$i]);
  44. }
  45. }
  46. }
  47.  
  48. return $dateCounts;
  49.  
  50. }
  51.  
  52. }
  53.  
  54. $JD = new JsonDate();
  55. $dateCounts = $JD->getDateCounts();
  56. print_r($dateCounts);
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Array
(
    [1369706477] => 1
    [1369706479] => 2
    [1369706481] => 1
    [1369706483] => 0
    [1369706485] => 0
    [1369706487] => 3
    [1369706489] => 1
    [1369706491] => 0
)