Reputation: 23
I am trying to compare two dates in a column called " fecha_registro " , this column is varchar (100), I'm trying to compare him with this date code:
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "gimnasio";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
$from = "2010-02-06";
$to = "2015-11-08";
$consulta = mysql_query("SELECT id_cliente FROM clientes WHERE fecha_registro BETWEEN '$from' AND '$to'");
$cantidad = mysql_num_rows($consulta);
echo $cantidad;
?>
the problem is that you never find any record when multiple records that meet that pattern
The structure of the "clients" table is this :
And the records like this:
please , as I fix this ?
Upvotes: 0
Views: 705
Reputation: 458
You can try by this way
SELECT id_cliente FROM clientes WHERE
STR_TO_DATE(fecha_registro, '%Y-%m-%d')
BETWEEN '$from' AND '$to'
Upvotes: 0
Reputation: 1006
try this
SELECT id_cliente FROM clientes WHERE
DATE_FORMAT(STR_TO_DATE(fecha_registro, '%Y-%c-%e'), '%Y-%m-%d')
BETWEEN '$from' AND '$to'
Upvotes: 1