Forums
This topic is locked
Regarding insertion of date into MYSQL
17 Oct 2005 07:58:41 Deepa dev posted:
I have a trouble in insering date from PHP to MYSQL..$var = $_POST["date"]; //here i am getting the date from a datepicker
$rs1 = mysql_query(" insert into sam values('"$var''') ",$connection )or die ("cant do it"//here i am trying to insert the dynamic date value .
how to insert dynamic date value into MYSQL format...
thx in advance
Regards,
Deepa.
Replies
Replied 20 Oct 2005 15:21:03
20 Oct 2005 15:21:03 Roddy Dairion replied:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
I have a trouble in insering date from PHP to MYSQL..
$var = $_POST["date"]; //here i am getting the date from a datepicker
$rs1 = mysql_query(" insert into sam values('"$var''') ",$connection )or die ("cant do it"//here i am trying to insert the dynamic date value .
how to insert dynamic date value into MYSQL format...
thx in advance
Regards,
Deepa.
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
There's 2 way date can by passed into mysql table meaning 2 formats 1 is "YmdHis" e.g. is 20051020130455 this will happen if you put the type of this field as "timestamp". Now if you do put the type of this field as timestamp the date will be generated automatically everytime a new record is inserted. The second type if if you set your field type as datetime which will set it as such 2005-10-20 13:04:55 so "Y-m-d H:i:s" now this is the most recommended 1 but you'll have to pass a date from you php page everytime, i guess you know how to do that already using the date function.
p.s: you can also set the field as date or time depending how you will use it. In your case it should be set as time.
Now to answer to your question use this code:
always give your variable meaningful name for example here is call date so you could set your variable as
$post_date or $date
$date=$_POST['date']; //always use single quotes when doing $_POST.
$rs1 = mysql_query("insert into sam(table_name) values('$date')" or die ("cant do it"
job done.
hope it helps.
I have a trouble in insering date from PHP to MYSQL..
$var = $_POST["date"]; //here i am getting the date from a datepicker
$rs1 = mysql_query(" insert into sam values('"$var''') ",$connection )or die ("cant do it"//here i am trying to insert the dynamic date value .
how to insert dynamic date value into MYSQL format...
thx in advance
Regards,
Deepa.
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
There's 2 way date can by passed into mysql table meaning 2 formats 1 is "YmdHis" e.g. is 20051020130455 this will happen if you put the type of this field as "timestamp". Now if you do put the type of this field as timestamp the date will be generated automatically everytime a new record is inserted. The second type if if you set your field type as datetime which will set it as such 2005-10-20 13:04:55 so "Y-m-d H:i:s" now this is the most recommended 1 but you'll have to pass a date from you php page everytime, i guess you know how to do that already using the date function.
p.s: you can also set the field as date or time depending how you will use it. In your case it should be set as time.
Now to answer to your question use this code:
always give your variable meaningful name for example here is call date so you could set your variable as
$post_date or $date
$date=$_POST['date']; //always use single quotes when doing $_POST.
$rs1 = mysql_query("insert into sam(table_name) values('$date')" or die ("cant do it"
job done.
hope it helps.