How to Select Data Between Two Dates in PHP/MySQL

Submitted by: 
Language: 
Visitors have accessed this post 5929 times.

This tutorial will show you how to select mysql rows between two inputted dates. This tutorial does not include a good design but will give you knowledge on the said topic. I've also included 2 mysqli methods which are Object-oriented and Procedural in the comments. So, feel free to switch between them.

Creating our Database

First, we're going to create a database that contains our data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "between".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

  1. CREATE TABLE `login` (
  2. `logid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `username` VARCHAR(30) NOT NULL,
  4. `login_date` datetime NOT NULL,
  5. PRIMARY KEY(`logid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

dates

Inserting Data Into our Database

Next Step in to insert some data into our database. This will serve as our reference when we select our dates.
1. Click the database "between" that we have created earlier.
2. Click SQL and paste the code below.

  1. INSERT INTO `login` (`username`, `login_date`) VALUES
  2. ('nurhodelta', '2017-08-22 07:10:00'),
  3. ('lee', '2017-05-22 08:30:00'),
  4. ('nurhodelta', '2017-08-22 13:15:00'),
  5. ('lee', '2017-03-22 14:00:00'),
  6. ('nurhodelta', '2017-05-16 10:30:00'),
  7. ('lee', '2017-08-15 20:00:00');

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.

  1. <?php
  2.  
  3. //MySQLi Procedural
  4. //$conn = mysqli_connect("localhost","root","","between");
  5. //if (!$conn) {
  6. // die("Connection failed: " . mysqli_connect_error());
  7. //}
  8.  
  9. //MySQLi Object-oriented
  10. $conn = new mysqli("localhost","root","","between");
  11. if ($conn->connect_error) {
  12. die("Connection failed: " . $conn->connect_error);
  13. }
  14.  
  15. ?>

Creating our Form and Table

Lastly, we create our login table, our form and our result table on one page. To create the page, open your HTML code editor and paste the code below after the tag. We name this page as "index.php".

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>How to Select MySQL Table between Two Dates in PHP</title>
  5. </head>
  6. <body>
  7. <h2>Login Table</h2>
  8. <div>
  9. <table border="1">
  10. <thead>
  11. <th>UserID</th>
  12. <th>Username</th>
  13. <th>Login Date</th>
  14. </thead>
  15. <tbody>
  16. <?php
  17. include('conn.php');
  18. //MySQLi Procedural
  19. //$query=mysqli_query($conn,"select * from `login`");
  20. //while($row=mysqli_fetch_array($query)){
  21. /* ?>
  22. <tr>
  23. <td><?php echo $row['logid']; ?></td>
  24. <td><?php echo $row['username']; ?></td>
  25. <td><?php echo $row['login_date']; ?></td>
  26. </tr>
  27. <?php */
  28. //}
  29.  
  30. //MySQLi Object-oriented
  31. $query=$conn->query("select * from `login`");
  32. while($row = $query->fetch_array()) {
  33. ?>
  34. <tr>
  35. <td><?php echo $row['logid']; ?></td>
  36. <td><?php echo $row['username']; ?></td>
  37. <td><?php echo $row['login_date']; ?></td>
  38. </tr>
  39. <?php
  40. }
  41. ?>
  42. </tbody>
  43. </table>
  44. </div><br>
  45. <div>
  46. <form method="POST">
  47. <label>From: </label><input type="date" name="from">
  48. <label>To: </label><input type="date" name="to">
  49. <input type="submit" value="Get Data" name="submit">
  50. </form>
  51. </div>
  52. <h2>Data Between Selected Dates</h2>
  53. <div>
  54. <table border="1">
  55. <thead>
  56. <th>UserID</th>
  57. <th>Username</th>
  58. <th>Login Date</th>
  59. </thead>
  60. <tbody>
  61. <?php
  62. if (isset($_POST['submit'])){
  63. include('conn.php');
  64. $from=date('Y-m-d',strtotime($_POST['from']));
  65. $to=date('Y-m-d',strtotime($_POST['to']));
  66. //MySQLi Procedural
  67. //$oquery=mysqli_query($conn,"select * from `login` where login_date between '$from' and '$to'");
  68. //while($orow=mysqli_fetch_array($oquery)){
  69. /* ?>
  70. <tr>
  71. <td><?php echo $orow['logid']?></td>
  72. <td><?php echo $orow['username']?></td>
  73. <td><?php echo $orow['login_date']?></td>
  74. </tr>
  75. <?php */
  76. //}
  77.  
  78. //MySQLi Object-oriented
  79. $oquery=$conn->query("select * from `login` where login_date between '$from' and '$to'");
  80. while($orow = $oquery->fetch_array()){
  81. ?>
  82. <tr>
  83. <td><?php echo $orow['logid']?></td>
  84. <td><?php echo $orow['username']?></td>
  85. <td><?php echo $orow['login_date']?></td>
  86. </tr>
  87. <?php
  88. }
  89. }
  90. ?>
  91. </tbody>
  92. </table>
  93. </div>
  94. </body>
  95. </html>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

You are a blessing this is the last feature i need to include on my final project thanks for the tutorial.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.