【问题标题】:CodeIgniter - MySql timestamp/datetime keep giving me zeros when insertingCodeIgniter - MySql 时间戳/日期时间在插入时不断给我零
【发布时间】:2015-06-03 02:25:07
【问题描述】:

我目前正在使用 CodeIgniter 2.2.2,并且我的模态中有以下代码:

$this->createDate = time();
$this->db->insert('someDB_Table', $this);

我的 createDate 类型是时间戳(我也尝试过 datetime)。但这在插入数据库时​​给了我全零。还有其他方法可以解决这个问题吗?像这样:0000-00-00 00:00:00

【问题讨论】:

  • var_dump($this) 的输出是什么?

标签: php mysql codeigniter datetime timestamp


【解决方案1】:

试试这个(数组输入法)

 $my_array = array(
'database_field' => $this->input->post('input_form_field'),
'time_stamp'       => date('Y-m-d H:i:s'),
);
 $this->db->insert('someDB_Table', $my_array);

【讨论】:

    【解决方案2】:

    我修正了错误:

    this->createDate = date("Y-m-d H:i:s");
    

    显然 Datetime/Timestamp 需要在插入时及时识别格式。所以你去吧!

    【讨论】:

      【解决方案3】:

      看起来您将日期存储在变量“createDate”中,该变量存储在结构变量 $this 中。所以它与 $this['createDate'] = time(); 完全一样 尝试做:

      $this = time();
      $this->db->insert('someDB_Table', $this);
      

      或

      $this->createDate = time();
      $this->db->insert('someDB_Table', $this->createDate);
      

      我还没有测试过,但它们都应该可以工作。还要检查你的表列名(有时错误来自那个)。

      否则我几乎每次都是这样做的:

      $date = time();
      $mysqli->query("INSERT INTO `someDB_Table`(`createDate`) VALUES (`$date`)");
      

      祝你好运;)

      【讨论】:

      • 我的数据库也有其他字段。所以我真的不能将我的对象传递到整个表中
      猜你喜欢
      • 2019-12-13
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      相关资源
      最近更新 更多