Page 1 of 1

Problem - Query with dates

Posted: Fri Mar 12, 2010 4:53 am
by mariolopes
Hi
I need to do one query between 2 dates . I try :

Code: Select all

 
$query="select * from negocios where Data_venda BETWEEN '$_POST[datanicial]' AND '$_POST[datafinal]'";
$result=mysql_query($query);
while ($row=mysql_fetch_array($result)){
 
But it doesn't work. For example it returns empty dates (0000/00/00). What's wrong?
For example: If datainicial=2010-03-12 I'll get dates
0000-00-00
2010-03-11
2010-02-28
2010-03-12
if data inicial=2010-03-11 the results are
0000-00-00
2010-03-11
2010-02-28
Why?

Thank you

Re: Problem - Query with dates

Posted: Fri Mar 12, 2010 5:51 am
by mariolopes
Solved:
Here is the answer

Code: Select all

 
function formatDate($dDate){
 $dNewDate = strtotime($dDate);
 return date('Y/m/d',$dNewDate);
 }
$datita=formatDate($_POST[datainicial]);
$datitafinal=formatDate($_POST[datafinal]);
 
$query="select * from negocios where Data_venda BETWEEN '$datita' AND '$datitafinal'";
 
$result=mysql_query($query);
while ($row=mysql_fetch_array($result)){
echo "<tr>";
    echo"<td>";
    echo $row[Codigo];
    echo"</td>";
    echo"<td>";
    echo $row[Designacao];
    echo"</td>";
    echo"<td>";
    echo $row[Data_venda];
    echo"</td>";
    echo"<td>";
    echo $row[Preco_venda];
    echo"</td>";
    echo"<td>";
    echo $row[Preco_compra];
    echo"</td>";
    echo"<td>";
    echo "Lucro";
    echo"</td>";
echo"</tr>";    
}
 
echo "</table>";
}