CodeIgniter Forums
Cannot Get Date format from input field - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Cannot Get Date format from input field (/showthread.php?tid=70982)



Cannot Get Date format from input field - sanjaya - 06-24-2018

I am trying insert birth day from codeigniter form to mysql database as mysql date format, But i am getting like this format "06/27/2018". My model as below


PHP Code:
       
        $birth_day 
date('Y-m-d');
 
       $birth_day $this->input->post("b_day");

 
       $config['upload_path'         './uploads/member/';
 
       $config['allowed_types'       'gif|jpg|png';
 
       $config['max_size'            5000000000;
 
       $config['max_width'           50000;
 
       $config['max_height'          50000;

 
       $this->load->library('upload'$config); //Enable upload library and pass data as $config variable
 
       //upload image
 
       if ($this->upload->do_upload('photo'))
 
       {
 
           echo "File 1 Upload Success!";
 
           $upload_data $this->upload->data();
 
           $data['photo'] = "uploads/member/".$upload_data['file_name'];  //.$upload_data['file_name']; = Dispaly file name & upload path
 
       }else{
 
           echo $this->upload->display_errors();
 
       }

 
       $data['custom_member_id'] = $this->input->post("custom_member_id");
 
       $data['surname'] = $this->input->post("surname");
 
       $data['other_name'] = $this->input->post("other_name");
 
       $data['initials'] = $this->input->post("initials");
 
       $data['title'] = $this->input->post("title");
 
       $data['gender'] = $this->input->post("gender");
 
       $data['b_day'] = $birth_day;
 
       $data['category'] = $this->input->post("category");
 
       $data['nic'] = $this->input->post("nic");
 
       $data['occupation'] = $this->input->post("occupation");
 
       $data['address_01'] = $this->input->post("address_01");
 
       $data['address_02'] = $this->input->post("address_02");
 
       $data['city'] = $this->input->post("city");
 
       $data['district'] = $this->input->post("district");
 
       $data['zip_code'] = $this->input->post("zip_code");
 
       $data['h_phone'] = $this->input->post("h_phone");
 
       $data['email'] = $this->input->post("email");
 
       $data['of_phone'] = $this->input->post("of_phone");
 
       $data['mobile'] = $this->input->post("mobile");
 
       $data['religion'] = $this->input->post("religion");
 
       $data['age_group'] = $this->input->post("age_group");
 
       $data['join_date'] = $this->input->post("join_date");
 
       $data['last_renewal'] = $this->input->post("last_renewal");
 
       $data['status'] = $this->input->post("status");
 
       $data['created_by'] = $this->session->userdata("NAME");
 
       

        $this
->db->insert('membership'$data);

 
       $b_d['member_id'] = $this->db->insert_id();
 
       $b_d['name'] = $this->input->post("other_name");
 
       $b_d['b_date'] =   DATE($this->input->post("b_day"));

 
       $this->db->insert('birth_day'$b_d); 


After that I try to get birth day to reminder. But its not work with this date format. I am using bootstrap datetimepicker. 


RE: Cannot Get Date format from input field - Pertti - 06-25-2018

Browsers use different date format than MySQL servers. You have to convert it from one format to another before you add it to DB, but there are few helpful PHP functions that might solve this fairly easily:

PHP Code:
$birth_day date('Y-m-d'strtotime($this->input->post("b_day"))); 



RE: Cannot Get Date format from input field - sanjaya - 06-25-2018

(06-25-2018, 12:33 AM)Pertti Wrote: Browsers use different date format than MySQL servers. You have to convert it from one format to another before you add it to DB, but there are few helpful PHP functions that might solve this fairly easily:

PHP Code:
$birth_day date('Y-m-d'strtotime($this->input->post("b_day"))); 

Thanks Pertti,

It's work.


RE: Cannot Get Date format from input field - Pertti - 06-25-2018

You probably need to add some checks, because if b_day wasn't provided, strtotime will return false, and date in return will use it as 0 - which will give you dates in year 1970, but glad it worked Smile


RE: Cannot Get Date format from input field - sanjaya - 06-25-2018

(06-25-2018, 02:31 AM)Pertti Wrote: You probably need to add some checks, because if b_day wasn't provided, strtotime will return false, and date in return will use it as 0 - which will give you dates in year 1970, but glad it worked Smile

I am new to web development. Can you help me to resolve this error.


RE: Cannot Get Date format from input field - InsiteFX - 06-25-2018

You really should read up on the DateTime Class etc; On PHP.NET

Here is one example that will help you to check the datetime.

date_parse


RE: Cannot Get Date format from input field - sanjaya - 06-25-2018

(06-25-2018, 03:37 AM)InsiteFX Wrote: You really should read up on the DateTime Class etc; On PHP.NET

Here is one example that will help you to check the datetime.

date_parse


Now I am getting below error. 

"Array to string conversion"

This is my code. 


PHP Code:
value="<?php
                                
$j_date = date_parse($row->join_date);
                                echo 
$j_date;   ?> " 



RE: Cannot Get Date format from input field - Pertti - 06-25-2018

(06-25-2018, 04:42 AM)sanjaya Wrote: "Array to string conversion"

Did you read the documentation?

date_parse will return array of date values broken down into array, while date + strtotime will give you a string in format you specify.

We have pointed you to solution for original issue, which was reformatting dates that come in from browser and need to go in DB in different format.