Codeigniter - 输入数据库的日期/时间

时间:2013-03-21 21:15:11

标签: php codeigniter

我已经阅读了很多关于CI在使用活动记录时如何逃避NOW()函数的指南/页面等。

我试图利用变量中的函数,然后在我的数组中调用它,将各种字段输入到数据库中;但它似乎根本没有用。我尝试过的两种方法如下,第二种方法在数据库中被解释为0000-00-00;第一个将我的AJAX控制器放入一个循环中,导致根本没有添加任何数据..

谢谢!

尝试1

if ($this->form_validation->run() != FALSE) 
        {
            // Passed the form validation

            **$initiated_date =  $this->db->set('date', 'NOW()', FALSE);**  

            $order = array(


                'status'          => $this->input->post('status'), 
                'priority'        => $this->input->post('priority'), 
                'target_date'     => $this->input->post('target_date'),
                'initiated_date'  => $this->input->post('$initiated_date'),
                'requestor_name'  => $this->input->post('requestor_name'),
                'requestor_telno' => $this->input->post('telephone'),           
                'job_description'        => $this->input->post('job_desc'), 
                'trade_type'    => $this->input->post('trade')

            );

尝试2

 if ($this->form_validation->run() != FALSE) 
            {
                // Passed the form validation

                $initiated_date =  $this->db->set('date', 'NOW()', FALSE);  

                $order = array(


                    'status'          => $this->input->post('status'), 
                    'priority'        => $this->input->post('priority'), 
                    'target_date'     => $this->input->post('target_date'),
                    'initiated_date'  => $this->input->post('date', 'NOW()', FALSE), 
                    'requestor_name'  => $this->input->post('requestor_name'),
                    'requestor_telno' => $this->input->post('telephone'),           
                    'job_description'        => $this->input->post('job_desc'), 
                    'trade_type'    => $this->input->post('trade')

                );

工作解决方案

//sets variable and uses php date function
            $initiated_date =  date('Y-m-d');  

        $order = array(


            'status'          => $this->input->post('status'), 
            'priority'        => $this->input->post('priority'), 
            'target_date'     => $this->input->post('target_date'),
            'initiated_date'  => $initiated_date,
            'requestor_name'  => $this->input->post('requestor_name'),
            'requestor_telno' => $this->input->post('telephone'),           
            'job_description'        => $this->input->post('job_desc'), 
            'trade_type'    => $this->input->post('trade')

        );

1 个答案:

答案 0 :(得分:1)

CI input-> post函数用于从$ _POST(http://ellislab.com/codeigniter/user-guide/libraries/input.html)中提取数据

我认为这是你想要完成的事情:

'initiated_date'=> $ initiated_date

如果要将initiate_date设置为时间戳,可以使用php time()函数:$ initiated_date = time()