Insert date in Mysql via PHP

Asked

Viewed 30,995 times

1

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

I tried to find several places and nothing that would help me, because the date saved in the comic book is always nothing to do with the typed when I go to 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 don’t know if it interferes with the conversion, but I’m using a jQuery mask for the field DATE, is there a problem?

  • Other options, http://answall.com/questions/21774/comort-inverter-datas-no-php-independente-format

  • I tried the function that is in the link, but it didn’t work with me. Do you have another suggestion? in Mysql even with the function it saves with the system date in BD.

3 answers

2

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

implode('-', array_reverse(explode('/', $data)))
  • how do I insert this? put $data = implode('-', array_reverse(explode('/', $data))); would be so?

  • you can use this way you mentioned @Darkjontex!

  • already tried and does not work. It saved in Mysql with the system date. : / do not know what I do, I did so: $venc_garantia=date("d/m/Y");
 
 $venc_garantia = implode("-",array_reverse(explode("/",$venc_garantia)));

 $data=date("d/m/Y"); 

 $data = implode("-",array_reverse(explode("/",$data)));

  • @Darkjontex, what about the system date? You are passing the system date when you do this: "date("d/m/Y")"..

  • It’s like this, I get from the <form> which is an equipment registration form. In it I get it in the format (dd-mm-YYYY), and then I use its function. This is for Date and Warranty. With all this I made this script that I passed and didn’t solve my problem :/ what do you suggest? I just want to save from PHP that this in the dd-mm-YYYY format to the mysql format that is YYYY-mm-dd

  • If you are receiving the date in the format: dd-mm-YYYY, you will have to change the function I gave you. Make the following change from: explode ('/', $data) to: explode ('-', $data). See if it works!

  • managed @Darkjontex?

  • Not Weliton Junior. In fact, I expressed myself wrong, but it didn’t work anyway: my saved date is type dd/mm/YYYY (by Jquery mask). Next I want php to save this date in mysql in YYYY-MM-DD format. However, even using this form and the original, I did not succeed. Is it because the mask is Jquery? how do I make a date mask in PHP for HTML fields?

  • My current code looks like this: $venc_garantia=date("d/m/Y");&#xA; $venc_garantia = implode("-",array_reverse(explode('/', $venc_garantia)));&#xA;&#xA; $data=date("d/m/Y");&#xA; $data = implode("-",array_reverse(explode("/",$data)));

Show 4 more comments

1


I got it thanks to the hardware people: http://www.hardware.com.br/comunidade/data-duvida/902709/

For those who have the same doubt I had 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));`

ECHO is just to print the value on the page to see how the dates are coming out, but this ensures me to successfully insert it into mysql without problems. However I thank the help and help of all!

1

@Darkjontex, what I always do, which is very simple, is this:

//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];

OBS: Before giving an explosion, of an echo in the variable that receives the date coming from the field:

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

To see if the date is in the format dd/mm/yyyy or yyyy-mm-dd, right? I’ve always done this and it’s always worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.