problems with mysql and php dates

0

Greetings I'm trying to make an application for personnel control more than anything to be able to keep in what date I enter and in what date its contract ends and to keep its date of birth I have found much information on the datepicker.

It will sound silly but I prefer to ask you to stay with the doubt. How could I put a date picker on a form where the user can choose the dates they want. and how would such data be received in the php data insertion format?

Here I show you the code of how I am capturing this data what I do not remember is if when passing the info to the next module it is necessary to change the format or I can save directly

<label for="fechalta">Fecha de Alta</label>
<input type="date" name="fechainicio" step="1" min="2018-01-01" max="2018-12-31" value="<?php echo fechalta;?>">
    
asked by franksegovia 04.06.2018 в 23:50
source

2 answers

0

The dates are undoubtedly a topic haha here I leave some considerations:

First of all, check the format in which the DatePicker returns the selected date, since the database must have the same intention either DateTime (If it includes time) or Date (If it is only the date)

Hence, the best way I have found to avoid problems when entering the date in the database is to use the following MYSQL function directly in the Query:

STR_TO_DATE(string, format_mask)

Documentation: link

With it you can make use of the date as it is returned by the datepicker and MYSQL will be responsible for creating the string that MYSQL needs to save it correctly. The most used without doubt is:

STR_TO_DATE('$fecha_nacimiento', '%d-%m-%Y'),

If you have any questions, the documentation shows the other 'Format Mask' that you can use as needed.

I hope it will serve as a guide to face the fearful variables of the date type, which from time to time cause us nightmares.

    
answered by 05.06.2018 / 21:44
source
-1

Remember that dates regularly give them in a date("Y-m-d") format that is: 2018-05-01 so if you want to save the date of a datepicker you should check the format that you throw. It is easy to do the conversion in the php if you require a d-m-Y format.

    
answered by 05.06.2018 в 06:20