I am building a simple shopping cart using PHP and as per title i want to get the sum (total earnings) of price field of orders table but only for today date.
I created the following function but i can get the total earnings for the last day there were earnings, not today. For example if Today is Friday and the last day which there were earnings are Monday($180), then the result i get is Monday:$180 instead of Friday:$0 which is what i want.
Here is what i have now:
public function getTotalEarningsToday()
{
$records = (new Query())->select([
'sum(price) as count',
"from_unixtime(updated_at, '%Y-%m-%d') as day",
])
->from(Orders::tableName())
->orderBy('day desc')
->groupBy('day')
->indexBy('day')
->limit(1)
->where(['status' => Orders::STATUS_COMPLETED])
->all();
return $records;
}
I tried to store the current date in a variable and do something like that:
public function getTotalEarningsToday()
{
$todayDate = date("d.m.Y H:i");
$records = (new Query())->select([
'sum(price) as count',
"from_unixtime(updated_at, '$todayDate') as day",
])
->from(Orders::tableName())
->orderBy('day desc')
->groupBy('day')
->indexBy('day')
->limit(1)
->where(['status' => Orders::STATUS_COMPLETED])
->all();
return $records;
}
and now i get the today date inside the Array but still i can't get the right sum earnings records:
Array ( [count] => 61.35 [day] => 21.02.2022 09:18 ) )
It should be:
Array ( [count] => 0 [day] => 21.02.2022 09:18 ) )
Any help would be very much appreciated.