Jump to content


Photo

Consulta de rango de fechas en php y mysql


  • Please log in to reply
2 replies to this topic

#1 juanky

juanky

    Newbie

  • Miembros
  • Pip
  • 1 posts

Posted 29 May 2014 - 02:04 PM

Agradecería muchísimo si me pudieran ayudar con este gran problema, no me sale la consulta del total de registros entre un rango de fechas, he dejado el código que tengo hasta ahora, bueno desde ya muchas gracias.



delphi
  1. <?php
  2. require("../mod_configuracion/conexion.php");
  3. require("../temas/encabezado.php");
  4. ?>
  5. <html>
  6. <head>
  7.     <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  8. <title>Servicios Medicos</title>
  9. <link rel="stylesheet" type="text/css" href="tcal.css" />
  10. <script type="text/javascript" src="tcal.js"></script>
  11. <center><form action="Index.php" method="get">
  12. From : <input type="text" name="d1" class="tcal" value="" /> To: <input type="text" name="d2" class="tcal" value="" /> <input type="submit" value="Buscar">
  13. </form></center>
  14. <br><br><br>
  15. <center><table id="resultTable" data-responsive="table" style="text-align: left; width: 400px;" border="1" cellspacing="0" cellpadding="4">
  16. <thead>
  17. <tr>
  18. <th> Nombre </th>
  19. <th> Fecha </th>
  20. <th> Prueba </th>
  21. </tr>
  22. </thead>
  23. </center>
  24. <tbody>
  25. <?php
  26. include('connect.php');
  27. /*mysql_query("SET NAMES 'utf8'");
  28.                 mysql_set_charset('utf8');*/
  29. if (isset($_GET["d1"])) { $d1  = $_GET["d1"]; } else { $d1=0; };
  30. if (isset($_GET["d2"])) { $d2  = $_GET["d2"]; } else { $d2=0; };
  31. $result = $db->prepare("SELECT * FROM consulta WHERE fecha BETWEEN :a AND :b");
  32. $result->bindParam(':a', $d1);
  33. $result->bindParam(':b', $d2);
  34. $result->execute();
  35. for($i=0; $row = $result->fetch(); $i++){
  36. ?>
  37. <tr class="record">
  38. <td><?php echo $row['NombrePaciente']; ?></td>
  39. <td><?php echo $row['Fecha']; ?></td>
  40. <td><?php echo $row['PruebaRealizada']; ?></td>
  41. </tr>
  42. <?php
  43.  
  44. $start_ts = strtotime($d1); // start time stamp
  45. $end_ts = strtotime($d2); // end time stamp
  46. $day_sec = 86400;
  47. $interval = ($end_ts – $start_ts)/$day_sec; // number of days
  48. echo "el rango es de $interval días\n”;
  49. $count = 0;
  50. $working_ts = $start_ts;
  51. while ($working_ts < $end_ts) { // loop through each day to find saturdays
  52. $day = date('Sa', $working_ts);
  53. if ($day == 7) { // this is a saturday
  54. $count++;
  55. }
  56. $working_ts = $working_ts + $day_sec;
  57. }
  58. echo "Hay $count Sabados en este intervalo\n”;
  59.  
  60. $querys= mysql_query(“select count(*) from consulta where fecha between $start_ts AND $end_ts”);
  61. $total = mysql_num_rows($querys);
  62. echo ($total);
  63. }
  64. ?>
  65. <?php
  66.  if (isset($_POST["d1"])) { $d1  = $_POST["d1"]; } else { $d1=0; };
  67.  if (isset($_POST["d2"])) { $d2  = $_POST["d2"]; } else { $d2=0; };
  68.  $result = $db->prepare("SELECT count(fecha) FROM consulta WHERE fecha BETWEEN :a AND :b Group by fecha");
  69. $result->bindParam(':a', $d1);
  70.     $result->bindParam(':b', $d2);
  71.     $result->execute();
  72.     $numero = mysql_num_rows($result); // obtenemos el número de filas
  73.    
  74.     echo 'El Total de registros de la tabla es: '.$numero.'';  // imprimos en pantalla el número generado
  75.    
  76.     $query = mysql_query("select * from consulta");
  77.     $no_rows = mysql_num_rows($query);
  78.     echo 'El número de registros es: '.$no_rows.'';
  79.    
  80.     $query1="select count(fecha) from consulta where fecha between :a AND :b";
  81.     $total_count = mysql_num_rows($query1);
  82.     echo $total_count;
  83.     ?>


  • 0

#2 enecumene

enecumene

    Webmaster

  • Administrador
  • 7419 posts
  • LocationRepública Dominicana

Posted 07 June 2014 - 10:54 AM

Sólo te faltó colocar el Query mi estimado  :D

Saludos.
  • 0

#3 porfi.dev

porfi.dev

    Advanced Member

  • Miembros
  • PipPipPip
  • 183 posts
  • LocationMy House @ México

Posted 11 June 2014 - 04:29 PM

Solo trae la consulta, no lo hagas con MySQL_num_rows  o eso que utilizas

trae el arreglo y cuentas su tamaño con la funcion  count() de PHP, puedes ver mas información aquí:

http://php.net/manua...ction.count.php
  • 0




IP.Board spam blocked by CleanTalk.