Welcome Guest, Not a member yet? Register   Sign In
how to insert a current date into a database
#1

[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
#2

[eluser]Madmartigan1[/eluser]
www.php.net/date
#3

[eluser]m4d3 Gun[/eluser]
wish this can help u....


//front controller

<?
function save(){
if($this->input->post('submit')){
$now = date('Y-m-d H:iConfused'); //here u can set the datetime
$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">
&lt;input name="name" size="30" type="text" /&gt;&lt;/td>
</tr>

<tr>
<td width="138">Address</td>
<td width="372">
&lt;input name="address" size="30" type="text" /&gt;&lt;/td>
</tr>
<p>&lt;input name="submit" type="submit" value="Save" /&gt;&lt;/p>
</table>
&lt;/form&gt;
?&gt;
#4

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

[eluser]Unknown[/eluser]
thank you
#6

[eluser]renownedmedia[/eluser]
Code:
INSERT INTO table SET datecol = NOW()
#7

[eluser]laytone[/eluser]
[quote author="m4d3" date="1253101145"]wish this can help u....


//front controller

&lt;?
function save(){
if($this->input->post('submit')){
$now = date('Y-m-d H:iConfused'); //here u can set the datetime
$data = array(
'name'=>$this->input->post('name'),
'address'=>$this->input->post('address'),
'ts_created'=>$now
);

$this->db->insert('userdtails',$data)
redirect('front/index','refresh');

?&gt;


//than in the view

&lt;?

&lt;?=form_open('front/save');?&gt;
<table width="522">
<tr>
<td width="138">Name</td>
<td width="372">
&lt;input name="name" size="30" type="text" /&gt;&lt;/td>
</tr>

<tr>
<td width="138">Address</td>
<td width="372">
&lt;input name="address" size="30" type="text" /&gt;&lt;/td>
</tr>
<p>&lt;input name="submit" type="submit" value="Save" /&gt;&lt;/p>
</table>
&lt;/form&gt;
?&gt;[/quote]

Using this method make sure that your database field is a DateTime and not just a date field.




Theme © iAndrew 2016 - Forum software by © MyBB