Page 1 of 1

Date format in Mysql

Posted: Fri Mar 12, 2010 10:37 am
by mariolopes
Hi
This problem drives me crazy. I try to update a field type date but i can't and i don't know why. Here is my code:

Code: Select all

 
<?php
$mysql_id = mysql_connect('localhost', 'xxx', 'xxx');
mysql_select_db('mariolopes',$mysql_id);
 
$query="update negocios set Data_venda='$_POST[hoje]' where Codigo='$_POST[codigo]'";
$result=mysql_query($query);
if(!$result){
echo(mysql_error());
}
else{
    header("location:darcomopago.php");
}
 
?>
 
How can i get the date format in my server (d-m-y or y-m-d or ...)?
Thank you

Re: Date format in Mysql

Posted: Fri Mar 12, 2010 11:01 am
by mariolopes
It works like this

Code: Select all

 
<?php
$mysql_id = mysql_connect('localhost', 'mariolopes', 'freixo');
mysql_select_db('mariolopes',$mysql_id);
$lixo=$_POST['hoje'];
$bolas=explode ("/",$lixo);
$dia=$bolas[0];
$mes=$bolas[1];
$ano=$bolas[2];
$data_venda="'".$ano."-".$mes."-".$dia."'";
$query="update negocios set Vendido='S', Preco_venda='$_POST[preco]', Data_venda=$data_venda where Codigo='$_POST[codigo]'";
$result=mysql_query($query);
if(!$result){
echo(mysql_error());
}
else{
    header("location:darcomopago.php");
}
 
?>
 
What a headache.

Re: Date format in Mysql

Posted: Fri Mar 12, 2010 11:03 am
by lshaw
What date format are you submitting in. Assuming you are submitting 24th jan 2009 or vitually any date format, and your mysql field type is DATE and not DATETIME

Code: Select all

 
//validate the time here
$time=strtotime($_POST['hoje']); //change to php format 1234567890
$date=date("Y-m-d",$time); //change to mysql format
//insert it here
 

Re: Date format in Mysql

Posted: Sun Mar 14, 2010 1:40 pm
by mariolopes
Thank you
I'll try it