如何从使用 UNIX EPOCH 时间的数据库中查询当前和上个月的数据?
How do I query current and previous months data from a DB that uses UNIX EPOCH time?
我正在尝试查询采用 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');
那我该怎么做呢?
当您不过滤任何内容时,这就是集合的样子。如您所知,'time' 处于 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');
你也许可以在一个月内做这样的事情:
$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;
我正在尝试查询采用 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');
那我该怎么做呢? 当您不过滤任何内容时,这就是集合的样子。如您所知,'time' 处于 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');
你也许可以在一个月内做这样的事情:
$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;