用php计算两次时间差,然后加到总时间类型 [英] Calculating the difference between two times using php, and then adding it to a total of time type

查看:21
本文介绍了用php计算两次时间差,然后加到总时间类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我试图获得一项工作所花费的总时间(使用数据库字段 job_start 和 job_end time(7))然后将其添加到另一个模型的字段中,

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,

我的代码是

 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 执行 SQL 语句失败:SQLSTATE[22007]: [Microsoft][SQL Server Native Client 11.0][SQL Server]从字符串转换日期和/或时间时转换失败..

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..

任何关于正确方法的想法?即我是否要增加时间并计算差异?

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

推荐答案

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

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.

要进行差异化,您需要先进行转换.查看 CDateFormatter 并使用它来将您的属性转换为允许算术的数字表示.

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.

如果你不能让 Xdebug 工作,那么花时间让 Yii::log() 函数工作(它会默认输出到 runtime/application.log.

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.

这篇关于用php计算两次时间差,然后加到总时间类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆