Welcome Guest, Not a member yet? Register   Sign In
Cannot Get Date format from input field
#1

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. 
Reply
#2

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"))); 
Reply
#3

(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.
Reply
#4

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
Reply
#5

(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.
Reply
#6

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
What did you Try? What did you Get? What did you Expect?

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

(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;   ?> " 
Reply
#8

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB