A water meter has deactivation/activation: calculate the number of days in the current month that are actually used

introduction

A water meter is a device used to measure water consumption and is very important for water bill calculations. However, in some cases, the water meter may be deactivated or restarted, which presents some challenges in calculating the actual number of days used in the month. This article will explore how to handle this situation so that your water bill can be calculated accurately.

background

When a water meter is deactivated, it does not record any data. And when the water meter restarts, it will continue to record water consumption. Therefore, we need to exclude data from periods of downtime when calculating the true number of days used in the month.

solution

To calculate the actual number of days used in the current month, we can use the following steps:

Here's an example code snippet to demonstrate how to calculate the actual number of days used in the current month:

			$startDate = strtotime(date("Y-m-01"));
			$endDate = strtotime(date("Y-m-01")."+1 month");
			 //  计算最后一个状态
  			$lastStatus = TableStopLogModel::create()
                    ->where('date_time', $startDate, '<')
                    ->scalar("status") ?? 0;

            // 计算天数
            $logList = TableStopLogModel::create()
                ->where('date_time', $startDate, '>=')
                ->where('date_time', $endDate, '<')
                ->order('date_time', 'asc')->all();
            $stopDay = 0; // 停用天数
            $indexDate = $startDate;  // 1号开始
            $useDay = 0; // 使用天数
            //  10 为停用  20 为 启用
            foreach ($logList as $v) {
    
    
				// 这里可以合并优化, 为了方便查看分开
                if ($lastStatus == 0 && $v['status'] == 10) {
    
      
                	// 当月之前没有记录,第一条记录是启用,则默认第一条记录之前为停用
                    $stopDay += abs(($v['date_time'] - $indexDate) / 86400);
                } else if ($lastStatus == 0 && $v['status'] == 10) {
    
     
                	// 当月之前没有记录,第一条记录是停用,则默认第一条记录之前为启用
                    $useDay += abs(($v['date_time'] - $indexDate) / 86400);
                } else if ($v['status'] == 20 && $lastStatus == 10) {
    
     
                	//  从停用 到启用,  则 停用日期增加
                    $stopDay += abs(($v['date_time'] - $indexDate) / 86400);
                } else if ($v['status'] == 10 && $lastStatus == 20) {
    
     
                	 //  从启用到停用,  则 启动日期增加
                    $useDay += abs(($v['date_time'] - $indexDate) / 86400);
                }
                $indexDate = $v['date_time'];
                $lastStatus = $v['status'];
            }
            if ($lastStatus == 0){
    
    
                // 没有所有数据时 , 则自行判断视为当月默认启用还是停用
                
            }else if ($lastStatus == 20) {
    
     
            	// 最后一条是启用 ,将当月最后的日期视为启用
                $useDay += abs(($endDate - $indexDate) / 86400);
            } else if ($lastStatus == 10) {
    
    
                //  最后一条是停用 ,将当月最后的日期视为停用
                $stopDay += abs(($endDate - $indexDate) / 86400);
            }
            var_dump("停用天数" . $userDay);
            var_dump("使用天数" . $tempUserDay);

Guess you like

Origin blog.csdn.net/abc564643122/article/details/131492225