Query Data From Database Using Date Range in PHP/MySQL

This tutorial will teach you on how to query data base on two dates using PHP/MySQL between query. This tutorial is helpful if your are creating your system that needs sales report base on the range of date. To further understand this tutorial follow the steps bellow.

Creating Our Database

First we are going to create our database which stores our data. To create a database: 1. Open phpmyadmin 2. Then create database and name it as "tutorial". 3. After creating a database name, click the SQL and paste the following code.
  1. CREATE TABLE IF NOT EXISTS `birthday` (
  2.   `id` int(11) NOT NULL AUTO_INCREMENT,
  3.   `date` varchar(100) NOT NULL,
  4.   `name` varchar(100) NOT NULL,
  5.   `gender` varchar(100) NOT NULL,
  6.   PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;

Creating our Database Connection

Next step is to create a database connection and save it as "connect.php". In this Step, we will write our connection script in PDO format.
  1. <?php
  2. /* Database config */
  3. $db_host                = 'localhost';
  4. $db_user                = 'root';
  5. $db_pass                = '';
  6. $db_database    = 'tutorial';
  7.  
  8. /* End config */
  9.  
  10. $db = new PDO('mysql:host='.$db_host.';dbname='.$db_database, $db_user, $db_pass);
  11. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12.  
  13. ?>

Creating Our Display

The code bellow will include our js file because i use tigra calendar for the popup dates in two textbox. This also includes our BETWEEN query that we are going to use to retrieve the data from database using the two dates input. copy the code bellow and save it as "index.php".
  1. <link rel="stylesheet" type="text/css" href="tcal.css" />
  2. <script type="text/javascript" src="tcal.js"></script>
  3. <form action="index.php" method="get">
  4. From : <input type="text" name="d1" class="tcal" value="" /> To: <input type="text" name="d2" class="tcal" value="" /> <input type="submit" value="Search">
  5. </form>
  6. <table id="resultTable" data-responsive="table" style="text-align: left; width: 400px;" border="1" cellspacing="0" cellpadding="4">
  7. <thead>
  8.         <tr>
  9.                 <th> Birtday </th>
  10.                 <th> Name </th>
  11.                 <th> Gender </th>
  12.         </tr>
  13. </thead>
  14. <tbody>
  15. <?php
  16. include('connect.php');
  17. if (isset($_GET["d1"])) { $d1  = $_GET["d1"]; } else { $d1=0; };
  18. if (isset($_GET["d2"])) { $d2  = $_GET["d2"]; } else { $d2=0; };
  19. $result = $db->prepare("SELECT * FROM birthday WHERE date BETWEEN :a AND :b");
  20. $result->bindParam(':a', $d1);
  21. $result->bindParam(':b', $d2);
  22. $result->execute();
  23. for($i=0; $row = $result->fetch(); $i++){
  24. ?>
  25. <tr class="record">
  26. <td><?php echo $row['date']; ?></td>
  27. <td><?php echo $row['name']; ?></td>
  28. <td><?php echo $row['gender']; ?></td>
  29. </tr>
  30. <?php
  31. }
  32. ?>
  33. </tbody>
  34. </table>
Hope this code will help you. The js file can be found the zip attachment.

Comments

Hi, how do you would use a count query between a range of dates? please :)

In reply to by Jhony (not verified)

"select a.`id`, a.`username`, b.login_timestamp,count(*) from users a left join `client_access_log` b on b.unique_id=a.unique_id where a.usertype='U' and `login_timestamp` between '2014-05-11%' and '2014-05-17%' GROUP BY a.`id`";

I have many database records but when I try to calculate by date in every 2 months not working! do you have any idea?
  1. <link rel="stylesheet" type="text/css" href="tcal.css" />
  2. <script type="text/javascript" src="tcal.js"></script>
  3. <form action="index.php" method="get">
  4. From : <input type="text" name="d1" class="tcal" value="" /> To: <input type="text" name="d2" class="tcal" value="" /> <input type="submit" value="Search">
  5. </form>
  6. <table id="resultTable" data-responsive="table" style="text-align: left; width: 400px;" border="1" cellspacing="0" cellpadding="4">
  7. <thead>
  8.         <tr>
  9.                 <th> CASTIG EMH </th>
  10.                         </tr>
  11. </thead>
  12. <tbody>
  13. <?php
  14. include('connect.php');
  15. if (isset($_GET["d1"])) { $d1  = $_GET["d1"]; } else { $d1=0; };
  16. if (isset($_GET["d2"])) { $d2  = $_GET["d2"]; } else { $d2=0; };
  17. $result = $db->prepare("SELECT *,SUM(Metoda_De_Plata) FROM EMH_rezervari WHERE Data_Si_Ora_Rezervarii BETWEEN :a AND :b");
  18. $result->bindParam(':a', $d1);
  19. $result->bindParam(':b', $d2);
  20. $result->execute();
  21. for($i=0; $row = $result->fetch(); $i++){
  22. ?>
  23. <tr class="record">
  24. <td><?php echo $row['SUM(Metoda_De_Plata)']; ?></td>
  25.  
  26. </tr>
  27. <?php
  28. }
  29. ?>
  30. </tbody>
  31. </table>

Add new comment