Welcome Guest, Not a member yet? Register   Sign In
Insert auto date
#1

Hello,

I am trying to insert automatic date but unable to do so.

mysql:

content_article
content_date
content_id
content_name


models\Pages_model.php


PHP Code:
public function insert_content()
 
       {
                
                
$data = array(
                 
   'content_id' => $this->input->post('content_id'),
                 
   'content_name'  => $this->input->post('content_name'),
                    
'content_article'  => $this->input->post('content_article')
                );
            
 
               return $this->db->insert('content'$data);
 
       

but I unable to see the actual current date printed there.  I wonder how to make it printed automatically just by inserting an article?
" If I looks more intelligence please increase my reputation."
Reply
#2

If you are explicitly want to add date in table then you can use below code


PHP Code:
$this->db->set('content_date''NOW()'FALSE);
 
   or 
$data
['content_date'] = date('Y-m-d H:i:s'); 

and if you want to add date automatically when article is inserted, then you need to set default value of date field as CURRENT_TIMESTAMP and its data type should be timestamp

hope this will help you
Reply
#3

This is the MySQL code to do it.

Code:
-- Date Created:

 `created_at` DATETIME   DEFAULT CURRENT_TIMESTAMP,

-- Date Updated:

 `updated_at` DATETIME   DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 06-22-2016, 12:48 AM by davy_yg.)

(06-21-2016, 09:46 PM)pravins Wrote: If you are explicitly want to add date in table then you can use below code


PHP Code:
$this->db->set('content_date''NOW()'FALSE);
 
   or 
$data
['content_date'] = date('Y-m-d H:i:s'); 

and if you want to add date automatically when article is inserted, then you need to set default value of date field as CURRENT_TIMESTAMP and its data type should be timestamp

hope this will help you


This is good.  But what if I only want to type the date without timestamp.  Can anyone help me? 


Pages_model.php



PHP Code:
public function insert_pages()
        {
                        
                $data 
= array(
                    'pages_name' => $this->input->post('pages_name'),
                    'create_date'  => date('Y-m-d'),
                    'pages_order' => $this->input->post('pages_order'),
                    'pages_content' => $this->input->post('pages_content')                
                
);
            
                
return $this->db->insert('pages'$data);
        


and I get this new error message:

Thank you for filling the information. It has been successful.

Pages Name: Home
Create Date:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: create_date
Filename: views/submitpages.php
Line Number: 45
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyProfileCI\application\views\submitpages.php
Line: 45
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyProfileCI\application\controllers\Cpages.php
Line: 107
Function: view
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyProfileCI\index.php
Line: 315
Function: require_once

Order: 2
" If I looks more intelligence please increase my reputation."
Reply
#5

(This post was last modified: 06-22-2016, 01:01 AM by pravins.)

(06-22-2016, 12:43 AM)davy_yg Wrote:
(06-21-2016, 09:46 PM)pravins Wrote: If you are explicitly want to add date in table then you can use below code


PHP Code:
$this->db->set('content_date''NOW()'FALSE);
 
   or 
$data
['content_date'] = date('Y-m-d H:i:s'); 

and if you want to add date automatically when article is inserted, then you need to set default value of date field as CURRENT_TIMESTAMP and its data type should be timestamp

hope this will help you


This is good.  But what if I only want to type the date without timestamp.  Can anyone help me? 


Pages_model.php



PHP Code:
public function insert_pages()
        {
                        
                $data 
= array(
                    'pages_name' => $this->input->post('pages_name'),
                    'create_date'  => date('Y-m-d'),
                    'pages_order' => $this->input->post('pages_order'),
                    'pages_content' => $this->input->post('pages_content')                
                
);
            
                
return $this->db->insert('pages'$data);
        


and I get this new error message:

Thank you for filling the information. It has been successful.

Pages Name: Home
Create Date:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: create_date
Filename: views/submitpages.php
Line Number: 45
Backtrace:
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyProfileCI\application\views\submitpages.php
Line: 45
Function: _error_handler
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyProfileCI\application\controllers\Cpages.php
Line: 107
Function: view
File: C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyProfileCI\index.php
Line: 315
Function: require_once

Order: 2



you can use MySQL DATE(NOW()) function instead of NOW() in

PHP Code:
$this->db->set('content_date''DATE(NOW())'FALSE); 

also your field name is 'content_date' or 'create_date'
Reply




Theme © iAndrew 2016 - Forum software by © MyBB