php 在午夜之后的两个日期之间未能获得每日销售额-- codeigniter 4

4nkexdtk  于 2023-01-08  发布在  PHP
关注(0)|答案(2)|浏览(79)

我无法检索每日销售在我的codeigniter 4 pos系统午夜后,当查询两个日期。
POS系统在酒吧/俱乐部工作,开始时间为14:00,结束时间为03:00(次日)。
我目前的代码只显示每天的销售到午夜。午夜后,我的营业时间改变到新的一天返回什么,因为没有销售到那时。
以下是我尝试的方法

型号

//Get Daily Sales For Tenant ID - By Specific Opening/Closing time
    public function getdailySalesByTenant($t, $ot, $ct)
    {
        $builder = $this->db->table('orders as o');
        $builder->select('o.*, u.first_name as waiter');
        $builder->join('users as u', 'u.id = o.sold_by', 'left' );
        $builder->where('o.tenant_id', $t);
        $builder->where('o.created_at >=', $ot); 
        $builder->where('o.created_at <=', $ct);
        return $results = $builder->get()->getResult();
    }

控制器

//Daily Sales
    public function getdailySales()
    {
        $t = $this->settingsModel->where('user_id', $this->tenantId->tenant_id)->first();
        $ot = date('Y-m-d H:i:s', strtotime($t['opening_time']));
        $ct = date('Y-m-d H:i:s', strtotime($t['closing_time'].'+ 1 day'));
        
        $data = $this->transactionsModel->getdailySalesByTenant($this->tenantId->tenant_id, $ot, $ct);
        $response = [
                'success' => true,
                'data' => $data,
        ];
        return $this->response->setJSON($response);
    }

我想记录2023年1月5日14:00:00到2023年1月6日03:00:00的日销售额

4urapxun

4urapxun1#

代替:

// ...
$ot = date('Y-m-d H:i:s', strtotime($t['opening_time']));
$ct = date('Y-m-d H:i:s', strtotime($t['closing_time'].'+ 1 day'));
// ...

使用这个:

$ot = (new DateTime($t['opening_time']))->format("Y-m-d H:i:s");

$ct = (function () use ($t) {
    $openingTime = strtotime($t['opening_time']);
    $closingTime = strtotime($t['closing_time']);

    return ($closingTime >= $openingTime)
        ? (new DateTime($t['closing_time']))->format("Y-m-d H:i:s")
        : (new DateTime($t['closing_time']))->modify("+1 DAY")->format("Y-m-d H:i:s");
})();

参考文献:
1.日期时间类

  1. Adding one day to a date
gstyhher

gstyhher2#

当我使用if --- else时,它工作了。下面是控制器。

//Daily Sales
public function getdailySales()
{
    $t = $this->settingsModel->where('user_id', $this->tenantId->tenant_id)->first();
    
    $opening_hour = $t['opening_time'];
    $hour=date('H'); //24hr clock.
    if($hour < $opening_hour) {
        $ot = date('Y-m-d H:i:s', strtotime($t['opening_time'].'- 1 day'));
        $ct = date('Y-m-d H:i:s', strtotime($t['closing_time'].'+ 1 day'));
    } else {
        $ot = date('Y-m-d H:i:s', strtotime($t['opening_time']));
        $ct = date('Y-m-d H:i:s', strtotime($t['closing_time'].'+ 1 day'));
    }
    
    $data = $this->transactionsModel->getdailySalesByTenant($this->tenantId->tenant_id, $ot, $ct);
    $response = [
            'success' => true,
            'data' => $data,
    ];
    return $this->response->setJSON($response);
}

相关问题