Problem with date in mysql query


I have a problem with a rebellious date. It turns out that I have to make a query between two dates, which would be this:

SELECT id FROM 'Pedidos' WHERE ('fecha_cobro' BETWEEN '$fechini' AND '$fechafin') AND borrado = 0

I come from a form that I select the dates that I want to appear in the search. And I convert the date from Spanish to American format so that mysql understands them and I do it like this:


But it turns out that $ date has a life of its own and only works in certain cases, for example if I put 01/01/2018, but if I put today's date it says that your aunt. I have tried to put single quotes, double quotes, magic quotes, call one of quotation marks to see if he knows anything. But nothing I can not see the error. On the other hand, if I put the date directly in the query, it always works, so I deduce that the ruling is in date.

On the other hand, I have tried to show that it brings that post in case the error was in the form, but no, it brings the correct date (in Spanish format).

Apart from going to throw a shoe Rajoy what do you think I could do?

Thank you very much for your help.

asked by Killpe 01.12.2017 в 11:25

1 answer


Good Killpe,

The problem is that one format uses a separator ( Formato español '/' ) and the other uses a different one ( Formato inglés '-' ).

You must put a replace for the separators to be able to cast the date correctly, in the following way:

$fechafin=date('Y-m-d',strtotime(str_replace('/', '-',$_POST["Listado_fechafin"])));
answered by 01.12.2017 / 11:47