• We just launched and are currently in beta. Join us as we build and grow the community.

How to Select Data Between Two Dates in PHP/MySQL

forresttt

Physical Access Auditor
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
57
Likes
80
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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;

database_6.png

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'

    ,

    '2020-08-22 07:10:00'

    )

    ,
  3. (

    'lee'

    ,

    '2020-05-22 08:30:00'

    )

    ,
  4. (

    'nurhodelta'

    ,

    '2020-08-22 13:15:00'

    )

    ,
  5. (

    'lee'

    ,

    '2020-03-22 14:00:00'

    )

    ,
  6. (

    'nurhodelta'

    ,

    '2020-05-16 10:30:00'

    )

    ,
  7. (

    'lee'

    ,

    '2020-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. //MySQLi Procedural
  3. //$conn = mysqli_connect("localhost","root","","between");
  4. //if (!$conn) {
  5. // die("Connection failed: " . mysqli_connect_error());
  6. //}

  7. //MySQLi Object-oriented
  8. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "between"

    )

    ;
  9. if

    (

    $conn

    ->

    connect_error

    )

    {
  10. die

    (

    "Connection failed: "

    .

    $conn

    ->

    connect_error

    )

    ;
  11. }

  12. ?>

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. //MySQLi Object-oriented
  30. $query

    =

    $conn

    ->

    query

    (

    "select * from `login`"

    )

    ;
  31. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  32. ?>
  33. <tr>
  34. <td><?php

    echo

    $row

    [

    'logid'

    ]

    ;

    ?>

    </td>
  35. <td><?php

    echo

    $row

    [

    'username'

    ]

    ;

    ?>

    </td>
  36. <td><?php

    echo

    $row

    [

    'login_date'

    ]

    ;

    ?>

    </td>
  37. </tr>
  38. <?php
  39. }
  40. ?>
  41. </tbody>
  42. </table>
  43. </div><br>
  44. <div>
  45. <form method="POST">
  46. <label>From: </label><input type="date" name="from">
  47. <label>To: </label><input type="date" name="to">
  48. <input type="submit" value="Get Data" name="submit">
  49. </form>
  50. </div>
  51. <h2>Data Between Selected Dates</h2>
  52. <div>
  53. <table border="1">
  54. <thead>
  55. <th>UserID</th>
  56. <th>Username</th>
  57. <th>Login Date</th>
  58. </thead>
  59. <tbody>
  60. <?php
  61. if

    (

    isset

    (

    $_POST

    [

    'submit'

    ]

    )

    )

    {
  62. include

    (

    'conn.php'

    )

    ;
  63. $from

    =

    date

    (

    'Y-m-d'

    ,

    strtotime

    (

    $_POST

    [

    'from'

    ]

    )

    )

    ;
  64. $to

    =

    date

    (

    'Y-m-d'

    ,

    strtotime

    (

    $_POST

    [

    'to'

    ]

    )

    )

    ;
  65. //MySQLi Procedural
  66. //$oquery=mysqli_query($conn,"select * from `login` where login_date between '$from' and '$to'");
  67. //while($orow=mysqli_fetch_array($oquery)){
  68. /* ?>
  69. <tr>
  70. <td><?php echo $orow['logid']?></td>
  71. <td><?php echo $orow['username']?></td>
  72. <td><?php echo $orow['login_date']?></td>
  73. </tr>
  74. <?php */
  75. //}

  76. //MySQLi Object-oriented
  77. $oquery

    =

    $conn

    ->

    query

    (

    "select * from `login` where login_date between '$from

    ' and '$to

    '"

    )

    ;
  78. while

    (

    $orow

    =

    $oquery

    ->

    fetch_array

    (

    )

    )

    {
  79. ?>
  80. <tr>
  81. <td><?php

    echo

    $orow

    [

    'logid'

    ]

    ?>

    </td>
  82. <td><?php

    echo

    $orow

    [

    'username'

    ]

    ?>

    </td>
  83. <td><?php

    echo

    $orow

    [

    'login_date'

    ]

    ?>

    </td>
  84. </tr>
  85. <?php
  86. }
  87. }
  88. ?>
  89. </tbody>
  90. </table>
  91. </div>
  92. </body>
  93. </html>

That's it, test your work if it works fine and if ever there's an error occurred please read the step from the beginning again or you can compare your codes to the code I have uploaded. Feel free also to comment below for any question or reactions.

Enjoy :)

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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top