• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My date when leave blank still save this format in my db:1970-01-01

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

$save_project = $this->input->post('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

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:
  `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`)

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.

so is it saving it as 1970-01-01 or is it saving it 0000-00-00?

[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.

@wondering Coder,

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


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

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

[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.

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:

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

[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/

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.