php - Laravel 5 / Carbon: Count number of records for specific range in a different timezone -
i store records in est timezone rather utc never need use utc. though have needed generate report in utc.
i count number of "clicks" on site, how grab yesterdays click total: click::where('created_at', '>=', carbon::now()->yesterday())->where('created_at', '<=', carbon::now()->startofday())->count();
that works great, need click total "yesterday in utc" -- there easy way using eloquent / carbon?
assuming records in database stored est mention need following.
// start , end times want in utc $start = carbon::yesterday('utc'); $end = carbon::yesterday('utc')->endofday(); // convert times est $start->timezone('est'); $end->timezone('est'); // query number of clicks, 'wherebetween' great little shortcut // querying on range click::wherebetween('created_at', [$start, $end])->count();
note carbon fluent api simplify to;
$start = carbon::yesterday('utc')->timezone('est'); $end = carbon::yesterday('utc')->endofday()->timezone('est');
depends entirely how want code read.
as aside carbon::now()
, carbon::yesterday()
builders use default timezone specified in php.ini if not supplied.
Comments
Post a Comment