使用php计算两次之间的差异,然后将其添加到总时间类型。

时间:2022-01-27 16:26:40

Hi I have a problem where I am trying to get a total time that a job has taken (using database fields job_start and job_end time(7)) then adding this to another model's field,

嗨,我有个问题,我想要得到一份工作的总时间(使用数据库字段job_start和job_end time(7)),然后将其添加到另一个模型的字段中,

the code i have is

我的代码是。

 if (isset($_POST['Jobs'])) {

        $model->attributes = $_POST['Jobs'];
        $model->setScenario('closejob');
        $model->status = 2; //set status to closed

        //date time difference - this is the part I need help with 
        $diff = $model->job_start - $model->job_end;
        //need to get customer model and add time diff to it 
        $customermodel = Customers::model()->findByPk($model->customer_ID);
        $customermodel->total_time = $customermodel->total_time + $diff;
        $customermodel->save();



        if ($model->save())
            $this->redirect(array('view', 'id' => $model->job_ID));
    }

I have tried string to time and other date functions but to no avail , the above code throws the following error

我已经尝试了一些时间和其他日期函数,但是没有效果,上面的代码抛出了以下错误。

CDbCommand failed to execute the SQL statement: SQLSTATE[22007]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Conversion failed when converting date and/or time from character string..

CDbCommand未能执行SQL语句:SQLSTATE[22007]: [Microsoft][SQL Server本机客户端11.0][SQL Server]转换失败时,从字符串转换日期和/或时间。

Any ideas on the proper way to do this ? i.e am I going about the adding of time and calculating the differences all wrong ?

有什么合适的方法吗?我。我是不是要把时间的增加和计算的差异都算错?

I think it has something to do with converting the string to a time format but I am unsure how to do this

我认为它与将字符串转换成时间格式有关,但我不确定如何做到这一点。

2 个解决方案

#1


0  

Maybe you have to do

也许你必须这样做。

$diff = $model->job_end - $model->job_start;

Instead of

而不是

$diff = $model->job_start - $model->job_end;

#2


0  

Unless you have an attached behavior for that model, it is likely that your job_start and job_end attributes are strings that are formatted according to MSSQL, assuming the column types are a date/time representation. This is certainly the way CActiveRecord models work with MySQL and Yii with DATETIME type.

除非您对该模型有附加的行为,否则很可能您的job_start和job_end属性是根据MSSQL格式化的字符串,假定列类型是一个日期/时间表示。这当然是CActiveRecord模型使用DATETIME类型的MySQL和Yii的方式。

To do differences, you would need to convert first. Take a look at CDateFormatter and use it to convert your attributes to numerical representations that allow for arithmetic.

要实现差异,首先需要转换。查看CDateFormatter,并使用它将您的属性转换成允许算术的数字表示。

If you cannot get Xdebug to work, then invest time in getting the Yii::log() function working (it will output to runtime/application.log by default.

如果您不能让Xdebug工作,那么就花时间来获得Yii::log()函数(它将输出到运行时/应用程序)。默认日志。

#1


0  

Maybe you have to do

也许你必须这样做。

$diff = $model->job_end - $model->job_start;

Instead of

而不是

$diff = $model->job_start - $model->job_end;

#2


0  

Unless you have an attached behavior for that model, it is likely that your job_start and job_end attributes are strings that are formatted according to MSSQL, assuming the column types are a date/time representation. This is certainly the way CActiveRecord models work with MySQL and Yii with DATETIME type.

除非您对该模型有附加的行为,否则很可能您的job_start和job_end属性是根据MSSQL格式化的字符串,假定列类型是一个日期/时间表示。这当然是CActiveRecord模型使用DATETIME类型的MySQL和Yii的方式。

To do differences, you would need to convert first. Take a look at CDateFormatter and use it to convert your attributes to numerical representations that allow for arithmetic.

要实现差异,首先需要转换。查看CDateFormatter,并使用它将您的属性转换成允许算术的数字表示。

If you cannot get Xdebug to work, then invest time in getting the Yii::log() function working (it will output to runtime/application.log by default.

如果您不能让Xdebug工作,那么就花时间来获得Yii::log()函数(它将输出到运行时/应用程序)。默认日志。