Test123
Test123

Reputation: 23

Compare dates in varchar with mysql and php

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 :

enter image description here

And the records like this:

enter image description here

please , as I fix this ?

Upvotes: 0

Views: 705

Answers (2)

Shaymol Bapary
Shaymol Bapary

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

Jah
Jah

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

Related Questions