将varchar中的日期与mysql和php进行比较

时间:2015-11-14 00:47:48

标签: php mysql

我正在尝试比较名为“fecha_registro”的列中的两个日期,此列是varchar(100),我正在尝试将他与此日期代码进行比较:

<?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;

?>

问题是,当满足该模式的多个记录

时,您永远找不到任何记录

“clients”表的结构如下:

enter image description here

这样的记录:

enter image description here

拜托,我解决了这个问题?

2 个答案:

答案 0 :(得分:1)

试试这个

 SELECT id_cliente FROM clientes WHERE  
 DATE_FORMAT(STR_TO_DATE(fecha_registro, '%Y-%c-%e'), '%Y-%m-%d')
 BETWEEN '$from' AND '$to'

答案 1 :(得分:0)

你可以这样试试

SELECT id_cliente FROM clientes WHERE  
STR_TO_DATE(fecha_registro, '%Y-%m-%d')
BETWEEN '$from' AND '$to'