PHP - Add Days To Date

In this tutorial we will create a Add Days To Date Using PHP. PHP is a server-side scripting language designed primarily for web development. PHP is a server-side scripting language designed primarily for web development. It is mostly used by a newly coders for its user friendly environment. So let's now do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_date, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn = new mysqli('localhost', 'root', '', 'db_date');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database");
  6.         }
  7.  
  8. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Add Days To Date</h3>
  16.                 <hr style="border-top:1px dotted #ccc;">
  17.                 <button class="btn btn-success" type="button" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Event</button>
  18.                 <br /><br />
  19.                 <table class="table table-bordered">
  20.                         <thead class="alert-info">
  21.                                 <tr>
  22.                                         <th>Event</th>
  23.                                         <th>Date</th>
  24.                                         <th>Add Days</th>
  25.                                 </tr>
  26.                         </thead>
  27.                         <tbody style="background-color:#fff;">
  28.                                 <?php
  29.                                         require 'conn.php';
  30.                                         $query = $conn->query("SELECT * FROM `event`");
  31.                                         while($fetch = $query->fetch_array()){
  32.                                 ?>
  33.                                 <tr>
  34.                                         <td><?php echo $fetch['event']?></td>
  35.                                         <td><?php echo $fetch['date']?></td>
  36.                                         <td>
  37.                                                 <form method="POST" action="add_day.php" class="form-inline">
  38.                                                         <input type="hidden" name="event_id" value="<?php echo $fetch['event_id']?>"/>
  39.                                                         <input type="number" name="day" min="1" step="0" style="width:80px;" class="form-control" required="required"/>
  40.                                                         <button name="add" class="btn btn-sm btn-warning">Add</button>
  41.                                                 </form>
  42.                                         </td>
  43.                                 </tr>
  44.                                 <?php
  45.                                         }
  46.                                 ?>
  47.                         </tbody>
  48.                 </table>
  49.         </div>
  50.        
  51.         <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  52.                 <div class="modal-dialog" role="document">
  53.                         <form action="save_event.php" method="POST" enctype="multipart/form-data">
  54.                                 <div class="modal-content">
  55.                                         <div class="modal-body">
  56.                                                 <div class="col-md-2"></div>
  57.                                                 <div class="col-md-8">
  58.                                                         <div class="form-group">
  59.                                                                 <label>Event</label>
  60.                                                                 <input type="text" name="event" class="form-control" required="required"/>
  61.                                                         </div>
  62.                                                         <div class="form-group">
  63.                                                                 <label>Date</label>
  64.                                                                 <input type="date" name="date" class="form-control" required="required"/>
  65.                                                         </div>
  66.                                                 </div>
  67.                                         </div>
  68.                                         <div style="clear:both;"></div>
  69.                                         <div class="modal-footer">
  70.                                                 <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  71.                                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  72.                                         </div>
  73.                                 </div>
  74.                         </form>
  75.                 </div>
  76.         </div>
  77. </body>
  78. <script src="js/jquery-3.2.1.min.js"></script>
  79. <script src="js/bootstrap.js"></script>
  80. </html>

Creating PHP Query

This code contains the php query of the application. This code contains to methods. It can store the data inputs to the database server and it can also add days to a specific date of events. To do that just copy and write this block of codes inside the text editor, then save it as shown below. save_event.php
  1. <?php
  2.         require_once 'conn.php';
  3.         date_default_timezone_set("Etc/GMT-8");
  4.        
  5.         if(ISSET($_POST['save'])){
  6.                 $event = $_POST['event'];
  7.                 $date = $_POST['date']." ".date("h:i:s");
  8.                
  9.                 $conn->query("INSERT INTO `event` VALUES('', '$event', '$date')") or die($conn->error());
  10.                
  11.                 echo "<script>alert('Data Inserted')</script>";
  12.                 echo "<script>window.location = 'index.php'</script>";
  13.         }
  14. ?>
add_day.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         date_default_timezone_set("Etc/GMT-8");
  5.        
  6.         if(ISSET($_POST['add'])){
  7.                 $event_id = $_POST['event_id'];
  8.                 $day = $_POST['day'];
  9.                
  10.                 $query = $conn->query("SELECT * FROM `event` WHERE `event_id` = '$event_id'") or die($conn->error());
  11.                 $fetch = $query->fetch_array();
  12.                
  13.                 $date = date("Y-m-d h:s:i", strtotime("+".$day." DAY".$fetch['date']));
  14.                
  15.                 $conn->query("UPDATE `event` SET `date` = '$date' WHERE `event_id` = '$event_id'") or die($conn->error());
  16.                
  17.                 echo "<script>alert('Days Added')</script>";
  18.                 echo "<script>window.location = 'index.php'</script>";
  19.         }
  20. ?>
There you have it we successfully created Add Days To Date using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment