CodeIgniter Forums

Full Version: My date when leave blank still save this format in my db:1970-01-01
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Wondering Coder[/eluser]
first of all, morning and hi to all CI fans.

Code:
$save_project = $this->input->post('save_project');
        
            if($save_project)
            {
                $project_info = array(
                    't_id' => $this->session->userdata('user_id'),
                    'proj_title' => $this->input->post('project_title'),
                    'proj_description' => $this->input->post('description'),
                    'proj_deadline' => date('Y-m-d',strtotime($this->input->post('proj_deadline'))),                
                    'docu_date' => date('Y-m-d',strtotime($docu_date)),
                    'ref_date' => date('Y-m-d',strtotime($this->input->post('ref_date'))),
                    'photos_date' => date('Y-m-d',strtotime($this->input->post('photos_date'))),
                    'videos_date' => date('Y-m-d',strtotime($this->input->post('videos_date'))),
                    'documentation' => $this->input->post('docu'),
                    'reflection' => $this->input->post('ref'),
                    'photos' => $this->input->post('photos'),
                    'videos' => $this->input->post('videos'),
                    'display' => 1
                );
                    $this->admin_db->save_project($item,$project_info);
                    //redirect('admin/projects');
            }

I have this code above that save the data's in my db. As you can see I have 5 dates, and a user
can choose to select a date or not. Now my problem is when the user submit/save the data's and
the date's leave as blank in my db it still save this format date: 1970-01-01

I have this date type in my db:
Code:
CREATE TABLE IF NOT EXISTS `spms_project` (
  `documentation` tinyint(1) NOT NULL DEFAULT '0',
  `docu_date` date NOT NULL DEFAULT '0000-00-00',
  `reflection` tinyint(1) NOT NULL DEFAULT '0',
  `ref_date` date NOT NULL DEFAULT '0000-00-00',
  `photos` tinyint(1) NOT NULL DEFAULT '0',
  `photos_date` date NOT NULL DEFAULT '0000-00-00',
  `videos` tinyint(1) NOT NULL DEFAULT '0',
  `videos_date` date NOT NULL DEFAULT '0000-00-00',
  `display` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`proj_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

and also I'm using a js calendar library, don't know if my calendar lib is causing this but I've looked in it,doesn't have a default date that will save 1970-01-01.

El Forum

[eluser]R_Nelson[/eluser]
so is it saving it as 1970-01-01 or is it saving it 0000-00-00?

El Forum

[eluser]Wondering Coder[/eluser]
its saving 1970-01-01 when the text field for calendar is blank or the user didn't choose a date.

El Forum

[eluser]jmadsen[/eluser]
@wondering Coder,

if you use the date formatting on a null value, it will default to 1970-01-01.

Ex.

Code:
echo date('Y-m-d',strtotime(null));

You need to exclude those lines if your user doesn't submit a date

El Forum

[eluser]Wondering Coder[/eluser]
how am I supposed to do that? Since I have 4 dates I don't want to use if-else condition since it will give pressure on my db. Also tried using empty and isset function but still the same, when the value is null it save 1970-01-01.
Also I cannot used CI validation for the reason that a user can and cannot choose to input dates.

El Forum

[eluser]jmadsen[/eluser]
what do you mean, "pressure on your db"?

you have four elements in your array, this one for example:

'photos_date' => date('Y-m-d',strtotime($this->input->post('photos_date')))

take it out of the array you have now, and only add it conditionally. something like:

Code:
if ($this->input->post('photos_date')){
     $project_info['photos_date'] = date('Y-m-d',strtotime($this->input->post('photos_date')));
}

Put that AFTER your current array setup. if $project_info['photos_date'] isn't there, it won't get passed in the INSERT statement, and the default will be used

El Forum

[eluser]Wondering Coder[/eluser]
hi jmadsen,

used if-else condition and figured out how not to mess up the code but I have slight problem could you take a look at my link/new thread: http://ellislab.com/forums/viewthread/192014/