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

问题描述 投票:0回答:1

我试图查询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');
laravel laravel-5 epoch
1个回答
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;
© www.soinside.com 2019 - 2024. All rights reserved.