如何从使用UNIX EPOCH时间的数据库中查询当前和前几个月的数据?

时间:2019-03-17 15:34:12

标签: laravel laravel-5 epoch

我正在尝试查询UNIX EPOCH时间格式的数据库,所以我不能使用任何我惯用的标准修饰符。我听说Carbon在这些事情上很有用,但是我仍然没有掌握它的用途。

    $date = Carbon::today();
    $this_months_values = (new $propdash->custommenuitems->monthly_real_time_feed)::where('time',$date->copy()->format('m'))->sum('data');
    $one_month_agos_values = (new $propdash->custommenuitems->monthly_real_time_feed)::sum('data');


    $two_month_agos_values = (new $propdash->custommenuitems->monthly_real_time_feed)::sum('data');
    $three_month_agos_values = (new $propdash->custommenuitems->monthly_real_time_feed)::sum('data');
    $four_month_agos_values = (new $propdash->custommenuitems->monthly_real_time_feed)::sum('data');
    $five_month_agos_values = (new $propdash->custommenuitems->monthly_real_time_feed)::sum('data');
    $six_month_agos_values = (new $propdash->custommenuitems->monthly_real_time_feed)::sum('data');
    $seven_month_agos_values = (new $propdash->custommenuitems->monthly_real_time_feed)::sum('data');
    $eight_month_agos_values = (new $propdash->custommenuitems->monthly_real_time_feed)::sum('data');
    $nine_month_agos_values = (new $propdash->custommenuitems->monthly_real_time_feed)::sum('data');
    $ten_month_agos_values = (new $propdash->custommenuitems->monthly_real_time_feed)::sum('data');
    $eleven_month_agos_values = (new $propdash->custommenuitems->monthly_real_time_feed)::sum('data');

那么我该怎么做呢? 当您不过滤任何内容时,集合就是这样。如您所知,“时间”以EPOCH UNIX时间表示。

Collection {#9512 ▼
  #items: array:2 [▼
    0 => Feed115 {#9511 ▼
      #connection: "emoncms"
      #table: "feed_115"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:2 [▼
        "time" => 1552716000
        "data" => 222.314
      ]
      #original: array:2 [▼
        "time" => 1552716000
        "data" => 222.314
      ]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }
    1 => Feed115 {#9510 ▼
      #connection: "emoncms"
      #table: "feed_115"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:2 [▼
        "time" => 1552629600
        "data" => 405.903
      ]
      #original: array:2 [▼
        "time" => 1552629600
        "data" => 405.903
      ]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    }
  ]
}

我尝试过,但是没有用:

    $date = Carbon::today();

    /* Yearly Realtime Consumption data feed */
    $startthis = Carbon::now()->startOfMonth()->timestamp;
    $endthis = Carbon::now()->endOfMonth()->timestamp;
    $this_months_values = (new $propdash->custommenuitems->monthly_real_time_feed)::where('time',[$startthis, $endthis])->sum('data');

1 个答案:

答案 0 :(得分:1)

您可能可以在一个月内执行以下操作:

$start = Carbon::parse('-5 months')->startOfMonth()->timestamp;
$end = Carbon::parse('-5 months')->endOfMonth()->timestamp;

$five_month_agos_values = YourModel::whereBetween('time', [$start, $end])->sum('data');

或者,如果您不喜欢解析字符串的想法:

$start = Carbon::now()->subMonths(5)->startOfMonth()->timestamp;