we were last talking about cronjobs and yii2...

<?php
namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use DateTime;
use DatePeriod;
use DateInterval;

class CronJobs extends ActiveRecord
{
    private static $executionTimeStart = 0;

    public static function tableName(): string
    {
        return '{{%cronjobs}}';
    }
    
    public function behaviors(): array
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['started_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['ended_at'],
                ],
            ],
        ];
    }
    
    public function rules(): array
    {
        return [
            [['controller', 'action', 'running', 'success'], 'required'],
            [['limit', 'offset', 'running', 'success', 'started_at', 'ended_at'], 'integer'],
            [['last_execution_time'], 'number'],
            [['controller', 'action'], 'string', 'max' => 255],
        ];
    }
    
    private static function startExecutionTimer(): void
    {
        self::$executionTimeStart = microtime(true);
    }
    
    private static function stopExecutionTimer(): float
    {
        return microtime(true) - self::$executionTimeStart;
    }

    public static function initDatesInRange(string $startDate, string $endDate, int $daysDifference = 1): array
    {
        $datesInRange = [];
        $begin = new DateTime($startDate);
        $end = new DateTime($endDate);
        $endModify = $end->modify('+' . $daysDifference . ' day');
        $interval = new DateInterval('P' . $daysDifference . 'D');
        $datesRange = new DatePeriod($begin, $interval, $endModify);
        foreach ($datesRange as $date) {
            $datesInRange[$date->getTimestamp()] = $date->format("Y-m-d");
        }
        
        return $datesInRange;
    }
    
    public static function initDatetimesInRange(string $startDateTime, string $endDateTime, int $hoursDifference = 1): array
    {
        $datesInRange = [];
        $begin = new DateTime($startDateTime);
        $end = new DateTime($endDateTime);
        $endModify = $end->modify('+' . $hoursDifference . ' hour');
        $interval = new DateInterval('PT' . $hoursDifference . 'H');
        $datesRange = new DatePeriod($begin, $interval, $endModify);
        foreach ($datesRange as $date) {
            $datesInRange[$date->getTimestamp()] = $date->format("Y-m-d H:i:s");
        }
        
        return $datesInRange;
    }
    
    public static function getDateRange(string $from, string $to, int $daysDifference = 1): array
    {
        return self::initDatesInRange($from, $to, $daysDifference);
    }
    
    public static function getDatetimeRange(string $from, string $to, int $hoursDifference = 1): array
    {
        return self::initDatetimesInRange($from, $to, $hoursDifference);
    }
    
    public static function getCron(string $controller, string $action): ?self
    {
        $cron = self::find()
            ->where(['controller' => $controller, 'action' => $action, 'success' => 0])
            ->orderBy(['id' => SORT_DESC])
            ->one();
            
        if ($cron === null) {
            $cron = new self();
            $cron->controller = $controller;
            $cron->action = $action;
        }
        
        return $cron;
    }
    
    public static function getPreviousCron(string $controller, string $action): ?self
    {
        return self::find()
            ->where(['controller' => $controller, 'action' => $action, 'success' => 1])
            ->orderBy(['id' => SORT_DESC])
            ->one();
    }
    
    public static function run(string $controller, string $action, int $limit, int $max): ?self
    {
        try {
            Yii::info("Starting cron job for: $controller/$action", __METHOD__);
            $current = self::getCron($controller, $action);
            self::startExecutionTimer();

            $current->limit = $limit;
            $current->running = 1;
            $current->success = 0;

            // Determine offset from previous cron
            $previous = self::getPreviousCron($controller, $action);
            $current->offset = ($previous === null || ($previous->offset + $limit) >= $max) 
                ? 0 
                : $previous->offset + $previous->limit;

            if ($current->save()) {
                Yii::info("Cron job initialized successfully.", __METHOD__);
                return $current;
            } else {
                Yii::error("Failed to save cron job.", __METHOD__);
                return null;
            }
        } catch (\Exception $e) {
            Yii::error("Exception during cron job initialization: " . $e->getMessage(), __METHOD__);
            return null;
        }
    }
    
    public function finish(): bool
    {
        try {
            $this->running = 0;
            $this->success = 1;
            $this->last_execution_time = self::stopExecutionTimer();

            if ($this->save()) {
                Yii::info("Cron job finished successfully, execution time: {$this->last_execution_time}", __METHOD__);
                return true;
            } else {
                Yii::error("Failed to finalize cron job.", __METHOD__);
                return false;
            }
        } catch (\Exception $e) {
            Yii::error("Exception while finalizing cron job: " . $e->getMessage(), __METHOD__);
            return false;
        }
    }
}

I send a bunch of code before, had you refactor it.. anyway, write comments for each function.