Schedule Posting System using PHP and MySQL

Most of the famous blogging platforms and social media websites like Blogger, WordPress and Facebook offers their users the ability to schedule a post. Now what exactly you mean by scheduling a post. Suppose you have written an article on «Christmas Celebrations» and tomorrow is Christmas but tomorrow you are going to be busy so you won’t get the time to post your article so what you will do you will schedule your post for tomorrow, by scheduling the post your article will automatically get posted on the specified time and date. So scheduling gives you the power of providing the content to your audience even when you are not available.

Things you will require to follow this tutorial

  • Good understanding of PHP and MySQL
  • Should have worked with a cross-platform web server such as XAMPP, WAMPP etc.

So in this tutorial, we will learn how to build such a Schedule System which will help you to Schedule your post on a specific date and time.

First, let us see the number of files and folders which we are going to create in this tutorial.

Schedule System using PHP and MySQL

Note: Create your files and folders in the same hierarchy as shown in above image.

Now let us see what each file is for.

  • Main.class.php: It is where all the main operations are going to take place, all the main functions will be defined inside it.
  • Connection.php: This file will contain all the details for creating a connection to the database.
  • index.php: This file is used to show the data from our database.
  • Insert.php: for inserting records into the database.

Now before starting anything, we will first understand how our System is going to work.

Schedule System working PHP and MySQL

Basically, when we fetch records from the database we will check if the date of that post is greater than today’s date or not if it is then we won’t show it on the homepage.

Now we got an idea about how our system is going to schedule the posts, so let’s start building it first step is to design our database. We are going to create a database called «schedule» which will consist of a single table called «posts» which we are going to use for holding our records.


The above query will create a database named «schedule», now we will create our table inside it.

CREATE TABLE posts( id int primary key auto_increment, content varchar(500), posted_on TIMESTAMP);

Now we have successfully created our database now let’s move on to the coding part(the badass stuff cool). We will first create our main.class.php file, just type in the below code in your main.class.php.

<?php class Schedule{   private $my_result;   private $conn;   function __construct($connection){       $this->conn = $connection;   }   function check_schedule($time){       date_default_timezone_set('Asia/Kolkata');       $today = date('Y-m-d H:i:s');       if($time > $today){           return false;       }       else{           return true;       }   }   function show_data(){       $i = 0;       $sql = "select * from posts ORDER BY id DESC";       $result = $this->conn->query($sql);       while($row = $result->fetch_assoc()){          if($this->check_schedule($row['posted_on'])){               $this->my_result['post'][$i] = $row['content'];               $this->my_result['time'][$i] = $row['posted_on'];               $i++;          }            }       return $this->my_result;   }   function insert_data($content,$time=""){       $time = ($time=="")?(null):($time);       $sql = "insert into posts values(null,?,?)";       $stmt = $this->conn->stmt_init();       if($stmt->prepare($sql)){           $stmt->bind_param("ss",$content,$time);           if($stmt->execute()){               echo "<script>alert('success');</script>";           }       }    } }  ?>

Now let’s see what each of the function in our Schedule class is doing.

  • check_schedule(): This function is checking whether the date associated with the record is greater than today’s date or not. If it is greater than it will return false else it will return true.
  • show_data(): This function will display records on to the home page after checking the dates by using the check_schedule() function.
  • insert_data(): This function will be used to insert the records into the database.

Now we will create our connection.php file which we will use to create a connection with our database.

<?php $user = "your username"; $db = "schedule"; $pass = "your password"; $host = "localhost"; $conn = new mysqli($host,$user,$pass,$db); ?>

We are going to use this file almost everywhere, so be sure not to go wrong with the above code.

Now let’s create our home page where we are going to display all our records, so just type(yeah! you heard it right I said «type») the below code in your index.php file.

<?php require "connection/connection.php"; ?> <?php require "class/main.class.php"; ?> <!DOCTYPE html> <html> <head> <title>Schedule System</title> <link type="text/css" rel="stylesheet" href="css/style.css" /> </head> <body> <div id="area"> <?php $obj = new Schedule($conn); $results = $obj->show_data(); for($i=0;$i<count($results['post']);$i++){       echo "<div class='post'>";       echo "<h3>".$results['post'][$i]."</h3>";       echo "<p><span>Posted on:</span> ".$results['time'][$i]."</p>";       echo "</div>"; } ?>  </div> </body>  </html>

In the above code, we have simply called the show_data() function from the Schedule class to fetch records from our «posts» table and then we are displaying the data using the loop.

Now we have a home page which is fetching data from the database but our home page is currently blank because we haven’t inserted anything into our database so we need something to insert a record into the database for that we will create a form to insert our content the form will contain a field for scheduling your post the field will be optional because everytime you are not going to schedule your post right.

Type the following code in your insert.php file

<?phprequire "connection/connection.php"; require "class/main.class.php"; $obj = new Schedule($conn); ?> <!doctype html> <html> <head> <title>POST-Schedule System</title> <link type="text/css" rel="stylesheet" href="css/style.css" /> </head> <body> <div id="area"> <form method="POST" action="insert.php"> <fieldset> <legend>Insert a new post</legend> <textarea name="content"  placeholder="Enter your post..."></textarea> <label>Schedule your post</label><br /> <input type="datetime-local" name="scheduleddate" value="<?php echo date('Y-m-d').'T'.date('H:i'); ?>"/><br /><br /> <input type="submit" name="post" value="publish" />  </fieldset>  </form>  </div> </body>  </html> <?php if(isset($_POST['post'])){   $content = $_POST['content'];   $time = $_POST['scheduleddate'];   $time = str_replace("T"," ",$time);   $time = $time.":00";   $obj->insert_data($content,$time); } ?>

In the above code, we have used our insert_data() function for inserting a new record into the database.

Note: The value obtained from the datetime-local input field is obtained in the format «Y-m-dThh:mm» we converted it into the format «Y-m-d hh:mm:ss» which is the TIMESTAMP format.

Now we let’s apply some little styling to our boring pages. type in the following code into your style.css file.

body{   margin:0;   padding:0; } form fieldset legend{   font-size: 20px !important;   font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important; } input[type="submit"]{   border:none;   outline:none;   padding:10px;   color:white;   background: royalblue;   border-radius: 3px; } textarea{   border:none;   outline:none; } #area{   max-width:500px;   width:100\%;   margin:auto; } .post{   padding:10px;   color:black;   border:1px solid gray;   margin:10px 0 0 0;   font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } span{   color:royalblue; }

Now we have successfully created our Scheduling System, try entering different dates and Scheduling your posts for upcoming dates. Your System will look similar as shown below.

schedule system php and mysql

schedule system using php and mysql

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook page. You can also comment your questions below.

Also, don’t forget to subscribe to our Newsletter.

If you like this article, then please share it and help us grow.

Preorder and Postorder Traversal of binary tree in Python
02 September 2018

Binary Tree in Python
02 September 2018

Image Sharpening by High Pass Filter using Python and OpenCV
17 August 2018

Explaining Register variables in C with examples
17 August 2018

C program to generate all combinations of N-Bit Binary String
10 July 2018

Data Autosave System using PHP, MySQL and AJAX
06 July 2018

Понравилась статья? Поделиться с друзьями:
Добавить комментарий