如何在Codeigniter中应用timestampdiff?

时间:2019-05-14 19:47:41

标签: php mysql sql codeigniter

美好的一天!

我是新手,正在编写程序,

我需要获取第二列之间的timestampdiff,数据类型为datetime-local。

下面是一个SQL查询,该语法采用合适的语法来获取中断时间

SELECT timestampdiff(hour,DateStarted,DateRestored) as Outage from network_outage

但是我不知道如何将其插入到我的数据库中 我正在使用codeigniter框架

请参见下面的型号代码

    function get_networkoutage($OutageDuration)
      {
        $this->db->select('timestampdiff(DateStarted,DateRestored) as OutageDuration',$OutageDuration);
          $query = $this->db->get('network_outage');
          return $query;
      }

下面是我的控制器代码

    $data['NetworkOutage'] = $this->network_model->get_networkoutage($OutageDuration);

在视图页面中回显NetworkOutage后的

它给了我一个错误

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: OutageDuration

Filename: controllers/Site.php

Line Number: 59

Backtrace:

File: D:\xampp\htdocs\itsystem\application\controllers\Site.php
Line: 59
Function: _error_handler

File: D:\xampp\htdocs\itsystem\index.php
Line: 315
Function: require_once
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DateStarted, DateRestored) as OutageDuration FROM `network_outage`' at line 1

SELECT timestampdiff(DateStarted, DateRestored) as OutageDuration FROM `network_outage`

Filename: D:/xampp/htdocs/itsystem/system/database/DB_driver.php

Line Number: 691

感谢您和最诚挚的问候,

1 个答案:

答案 0 :(得分:0)

我的答案是对enter link description here的引用,因为我不能将其添加为评论。

SELECT TIMESTAMPDIFF(SECOND,'2012-06-06 13:13:55','2012-06-06 15:20:18') 将返回7583。 作为第一个参数,您可以使用MICROSECOND(微秒),SECOND,MINUTE,HOUR,DAY,WEEK,MONTH,QUARTER或YEAR。 希望这会有所帮助