Insert date into MySQL via PHP

1

I want to insert a FORM in HTML that made the field DATE , however it is in the format dd/mm/yyyy and mysql only accepts yyyy-mm-dd . How do I convert a date to insert correctly in MySQL database using PHP?

I tried to find in many places and nothing to help me, because the data saved in the database is always nothing to do with the typed when I check.

Currently my code has the field DATA in this format:

$dataOld=date("d/m/Y");
$data=date("Y-m-d", strtotime($dataOld));

$venc_garantiaOld=date("d/m/Y");
$venc_garantia=date("Y/m/d", strtotime($venc_garantiaOld));

PS: I do not know if interferes with the conversion, but I'm using the form in jQuery one mask for the field DATE , will have problem

?     
asked by anonymous 29.07.2014 / 17:03

3 answers

1
___ erkimt ___ Insert date into MySQL via PHP ______ qstntxt ___

I want to insert a FORM in HTML that made the field %code% , however it is in the format %code% and mysql only accepts %code% . How do I convert a date to insert correctly in MySQL database using PHP?

I tried to find in many places and nothing to help me, because the data saved in the database is always nothing to do with the typed when I check.

Currently my code has the field %code% in this format:

'$data = $_POST['data'];
echo date('d-m-Y', strtotime($data));

$data = date("Y-m-d",strtotime(str_replace('/','-',$data)));  
echo date('Y-m-d', strtotime($data));


$venc_garantia = $_POST['venc_garantia'];
echo date('d-m-Y', strtotime($venc_garantia));

$venc_garantia = date("Y-m-d",strtotime(str_replace('/','-',$venc_garantia))); 
echo date('Y-m-d', strtotime($venc_garantia));'

PS: I do not know if interferes with the conversion, but I'm using the form in jQuery one mask for the field %code% , will have problem

?     
______ ___ azszpr27412

got thanks to hardware staff link

For those who have had the same doubts that the implementation I used was as follows:

'$data = $_POST['data'];
echo date('d-m-Y', strtotime($data));

$data = date("Y-m-d",strtotime(str_replace('/','-',$data)));  
echo date('Y-m-d', strtotime($data));


$venc_garantia = $_POST['venc_garantia'];
echo date('d-m-Y', strtotime($venc_garantia));

$venc_garantia = date("Y-m-d",strtotime(str_replace('/','-',$venc_garantia))); 
echo date('Y-m-d', strtotime($venc_garantia));'

The ECHO are just to print the value on the page to see the dates as they are leaving, but it assures me enter successfully in mysql smoothly. However appreciate the help and assistance of all!

    
______ ___ azszpr76358

@DarkJontex, what I always do and that is very simple, is the following:

%pre%

NOTE: Before one explodes, an echo in the variable that receives the arrival date field:

%pre%

To see if the date is in the format dd / mm / yyyy or yyyy-mm-dd, right? Always have and always worked.

    
______ ___ azszpr26969

After receiving the data field of your form, you will have to reverse the date, below a suggestion:

%pre%     
___
01.08.2014 / 14:52
1
___ erkimt ___ Insert date into MySQL via PHP ______ qstntxt ___

I want to insert a FORM in HTML that made the field %code% , however it is in the format %code% and mysql only accepts %code% . How do I convert a date to insert correctly in MySQL database using PHP?

I tried to find in many places and nothing to help me, because the data saved in the database is always nothing to do with the typed when I check.

Currently my code has the field %code% in this format:

//Pegue a data no formato dd/mm/yyyy
$data = $_POST['dataDoCampoHTML'];
//Exploda a data para entrar no formato aceito pelo DB.
$dataP = explode('/', $data);
$dataNoFormatoParaOBranco = $dataP[2].'-'.$dataP[1].'-'.$dataP[0];

//Na hora de pegar a data do BD e exibir na tela, faça a mesma coisa que fiz acima, porém troquei '-' por '/':
$data = $row['dataDoBanco'];
$dataP = explode('-', $data);
$dataParaExibir = $dataP[2].'/'.$dataP[1].'/'.$dataP[0];

PS: I do not know if interferes with the conversion, but I'm using the form in jQuery one mask for the field %code% , will have problem

?     
______ ___ azszpr27412

got thanks to hardware staff link

For those who have had the same doubts that the implementation I used was as follows:

$data = $_POST['dataDoCampo'];
echo $data.'<br>';

The ECHO are just to print the value on the page to see the dates as they are leaving, but it assures me enter successfully in mysql smoothly. However appreciate the help and assistance of all!

    
______ ___ azszpr76358

@DarkJontex, what I always do and that is very simple, is the following:

//Pegue a data no formato dd/mm/yyyy
$data = $_POST['dataDoCampoHTML'];
//Exploda a data para entrar no formato aceito pelo DB.
$dataP = explode('/', $data);
$dataNoFormatoParaOBranco = $dataP[2].'-'.$dataP[1].'-'.$dataP[0];

//Na hora de pegar a data do BD e exibir na tela, faça a mesma coisa que fiz acima, porém troquei '-' por '/':
$data = $row['dataDoBanco'];
$dataP = explode('-', $data);
$dataParaExibir = $dataP[2].'/'.$dataP[1].'/'.$dataP[0];

NOTE: Before one explodes, an echo in the variable that receives the arrival date field:

$data = $_POST['dataDoCampo'];
echo $data.'<br>';

To see if the date is in the format dd / mm / yyyy or yyyy-mm-dd, right? Always have and always worked.

    
______ ___ azszpr26969

After receiving the data field of your form, you will have to reverse the date, below a suggestion:

%pre%     
___
23.07.2015 / 20:57
0
___ erkimt ___ Insert date into MySQL via PHP ______ qstntxt ___

I want to insert a FORM in HTML that made the field %code% , however it is in the format %code% and mysql only accepts %code% . How do I convert a date to insert correctly in MySQL database using PHP?

I tried to find in many places and nothing to help me, because the data saved in the database is always nothing to do with the typed when I check.

Currently my code has the field %code% in this format:

implode('-', array_reverse(explode('/', $data)))

PS: I do not know if interferes with the conversion, but I'm using the form in jQuery one mask for the field %code% , will have problem

?     
______ ___ azszpr27412

got thanks to hardware staff link

For those who have had the same doubts that the implementation I used was as follows:

implode('-', array_reverse(explode('/', $data)))

The ECHO are just to print the value on the page to see the dates as they are leaving, but it assures me enter successfully in mysql smoothly. However appreciate the help and assistance of all!

    
______ ___ azszpr76358

@DarkJontex, what I always do and that is very simple, is the following:

%pre%

NOTE: Before one explodes, an echo in the variable that receives the arrival date field:

%pre%

To see if the date is in the format dd / mm / yyyy or yyyy-mm-dd, right? Always have and always worked.

    
______ ___ azszpr26969

After receiving the data field of your form, you will have to reverse the date, below a suggestion:

%pre%     
___
29.07.2014 / 17:10