controllers/AdminController.php
protected function timeCorrection($model, $attribute)
    {//echo __METHOD__."(model,'$attribute')";var_dump($model->$attribute);
        $resultFormat = 'Y-m-d H:i';
        $dateFormats = [
            'Y-m-d H:i',
            'Y-m-d G:i',
            'Y-m-j H:i',
            'Y-m-j G:i',
            'Y-n-d H:i',
            'Y-n-d G:i',
            'Y-n-j H:i',
            'Y-n-j G:i',
            //'Y-m-d', 'Y-m-j', 'Y-n-d', 'Y-n-j', // do not use - will add current time, not 00:00
        ];

        // correct if empty time
        //$model->$attribute = rtrim($model->$attribute, " :");
        $needCorrection = true;
        if (':' == substr($model->$attribute, -1)) {
            $model->$attribute = rtrim($model->$attribute, ':');
            $model->$attribute .= '00:00';
            $needCorrection = false;
        }

        foreach($dateFormats as $dateFormat) {
            $datetime = \DateTime::createFromFormat($dateFormat, $model->$attribute);
            if (!empty($datetime)) break;

        }
        if ($needCorrection && !empty($datetime) && !empty($model->timezoneshift)) {
            $unixDatetime = $datetime->getTimestamp();
            $unixDatetime += 60 * intval($model->timezoneshift);
            $datetime->setTimestamp($unixDatetime);
            $model->$attribute = $datetime->format($resultFormat);
        }//var_dump($model->$attribute);exit;
    }
protected function timeCorrection($model, $attribute)
    {//echo __METHOD__."(model,'$attribute')";var_dump($model->$attribute);
        $resultFormat = 'Y-m-d H:i';
        $dateFormats = [
            'Y-m-d H:i',
            'Y-m-d G:i',
            'Y-m-j H:i',
            'Y-m-j G:i',
            'Y-n-d H:i',
            'Y-n-d G:i',
            'Y-n-j H:i',
            'Y-n-j G:i',
            //'Y-m-d', 'Y-m-j', 'Y-n-d', 'Y-n-j', // do not use - will add current time, not 00:00
        ];

        // correct if empty time
        //$model->$attribute = rtrim($model->$attribute, " :");
        $needCorrection = true;
        if (':' == substr($model->$attribute, -1)) {
            $model->$attribute = rtrim($model->$attribute, ':');
            $model->$attribute .= '00:00';
            $needCorrection = false;
        }

        foreach($dateFormats as $dateFormat) {
            $datetime = \DateTime::createFromFormat($dateFormat, $model->$attribute);
            if (!empty($datetime)) break;

        }
        if ($needCorrection && !empty($datetime) && !empty($model->timezoneshift)) {
            $unixDatetime = $datetime->getTimestamp();
            $unixDatetime += 60 * intval($model->timezoneshift);
            $datetime->setTimestamp($unixDatetime);
            $model->$attribute = $datetime->format($resultFormat);
        }//var_dump($model->$attribute);exit;
    }
protected function timeCorrection ( $ model , $ attribute ) { //echo __METHOD__."(model,'$attribute')";var_dump($model->$attribute); $ resultFormat = 'Y-m-d H:i' ; $ dateFormats = [ 'Y-m-d H:i' , 'Y-m-d G:i' , 'Y-m-j H:i' , 'Y-m-j G:i' , 'Y-n-d H:i' , 'Y-n-d G:i' , 'Y-n-j H:i' , 'Y-n-j G:i' , //'Y-m-d', 'Y-m-j', 'Y-n-d', 'Y-n-j', // do not use - will add current time, not 00:00 ] ; // correct if empty time //$model->$attribute = rtrim($model->$attribute, " :"); $ needCorrection = true ; if ( ':' == substr ( $ model -> $ attribute , - 1 ) ) { $ model -> $ attribute = rtrim ( $ model -> $ attribute , ':' ) ; $ model -> $ attribute .= '00:00' ; $ needCorrection = false ; } foreach ( $ dateFormats as $ dateFormat ) { $ datetime = \ DateTime :: createFromFormat ( $ dateFormat , $ model -> $ attribute ) ; if ( ! empty ( $ datetime ) ) break ; } if ( $ needCorrection && ! empty ( $ datetime ) && ! empty ( $ model -> timezoneshift ) ) { $ unixDatetime = $ datetime -> getTimestamp ( ) ; $ unixDatetime += 60 * intval ( $ model -> timezoneshift ) ; $ datetime -> setTimestamp ( $ unixDatetime ) ; $ model -> $ attribute = $ datetime -> format ( $ resultFormat ) ; } //var_dump($model->$attribute);exit; }
Correct datetime field according to timezone
Correct datetime field according to timezone