how to insert a current date into a database |
[eluser]Unknown[/eluser]
hi . i want to insert the current date and time into the database . In database i was created a table named userdtails in that i created one field named ts_created of type datetime . In this field i have to insert date and time for the detail about the user . date and time in format of yyyy/mm/dd hrs/minutes/seconds please anyone help me
[eluser]m4d3 Gun[/eluser]
wish this can help u.... //front controller <? function save(){ if($this->input->post('submit')){ $now = date('Y-m-d H:i ![]() $data = array( 'name'=>$this->input->post('name'), 'address'=>$this->input->post('address'), 'ts_created'=>$now ); $this->db->insert('userdtails',$data) redirect('front/index','refresh'); ?> //than in the view <? <?=form_open('front/save');?> <table width="522"> <tr> <td width="138">Name</td> <td width="372"> <input name="name" size="30" type="text" /></td> </tr> <tr> <td width="138">Address</td> <td width="372"> <input name="address" size="30" type="text" /></td> </tr> <p><input name="submit" type="submit" value="Save" /></p> </table> </form> ?>
[eluser]n0xie[/eluser]
[quote author="sramkrish" date="1253098235"]hi . i want to insert the current date and time into the database . In database i was created a table named userdtails in that i created one field named ts_created of type datetime . In this field i have to insert date and time for the detail about the user . date and time in format of yyyy/mm/dd hrs/minutes/seconds please anyone help me[/quote] Most databases have an option to fill the current timestamp in a field when a new record is inserted. Presuming you use MySQL take a look here.
[eluser]laytone[/eluser]
[quote author="m4d3" date="1253101145"]wish this can help u.... //front controller <? function save(){ if($this->input->post('submit')){ $now = date('Y-m-d H:i ![]() $data = array( 'name'=>$this->input->post('name'), 'address'=>$this->input->post('address'), 'ts_created'=>$now ); $this->db->insert('userdtails',$data) redirect('front/index','refresh'); ?> //than in the view <? <?=form_open('front/save');?> <table width="522"> <tr> <td width="138">Name</td> <td width="372"> <input name="name" size="30" type="text" /></td> </tr> <tr> <td width="138">Address</td> <td width="372"> <input name="address" size="30" type="text" /></td> </tr> <p><input name="submit" type="submit" value="Save" /></p> </table> </form> ?>[/quote] Using this method make sure that your database field is a DateTime and not just a date field. |
Welcome Guest, Not a member yet? Register Sign In |