Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 即使丢失记录,每天的汇总统计数据_Php_Mongodb - Fatal编程技术网

Php 即使丢失记录,每天的汇总统计数据

Php 即使丢失记录,每天的汇总统计数据,php,mongodb,Php,Mongodb,试图获取过去x天的统计数据。 这是有效的,除了我没有数据的日子。 现在我可以用一个PHP脚本来解决这个问题,但如果当天没有记录,我宁愿让它在计数时显示“0” TLDR;我想从今天倒数到30天前,即使没有记录(0),也要获得每天的记录数 我的目标是在30天内得到这样的东西 ["docdata_errors"]=> array(3) { ["labels"]=> array(3) { [0]=> string(10) "2019-09

试图获取过去x天的统计数据。 这是有效的,除了我没有数据的日子。 现在我可以用一个PHP脚本来解决这个问题,但如果当天没有记录,我宁愿让它在计数时显示“0”

TLDR;我想从今天倒数到30天前,即使没有记录(0),也要获得每天的记录数

我的目标是在30天内得到这样的东西

  ["docdata_errors"]=>
  array(3) {
    ["labels"]=>
    array(3) {
      [0]=>
      string(10) "2019-09-10"
      [1]=>
      string(10) "2019-09-17"
      [2]=>
      string(10) "2019-09-19"
    }
    ["counts"]=>
    array(3) {
      [0]=>
      int(2)
      [1]=>
      int(4)
      [2]=>
      int(6)
    }
    ["background_colors"]=>
    array(3) {
      [0]=>
      string(20) "rgba(119,93,143,0.4)"
      [1]=>
      string(20) "rgba(32,150,160,0.4)"
      [2]=>
      string(19) "rgba(92,87,255,0.4)"
    }
  }

虽然由于我没有所有日子的数据,它将返回30个索引,除了那些没有记录的索引;那些我没有数据的对象。

您可以在DateTime对象上循环,而不是使用数据源在数组中创建日期。设置结束日期并从当前日期开始倒计时,直到到达该日期为止

像这样:

$endDate = new DateTime();
$endDate->modify('-29 days');

for($currentDate = new DateTime(); $currentDate > $endDate; $currentDate->modify('-1 day')) {
    // Check if there is data for the current day using $currentDate->format('Y-m-d') to 
    // compare against the date string in your data source.
    // If there is data, use it. Otherwise create an entry from the date in 
    // $currentDate and substitute 0 as the count.
}

我编辑了我的问题。有人有什么想法吗?
$endDate = new DateTime();
$endDate->modify('-29 days');

for($currentDate = new DateTime(); $currentDate > $endDate; $currentDate->modify('-1 day')) {
    // Check if there is data for the current day using $currentDate->format('Y-m-d') to 
    // compare against the date string in your data source.
    // If there is data, use it. Otherwise create an entry from the date in 
    // $currentDate and substitute 0 as the count.
}