Skip to main content

PHP - Simple Facebook "Time Ago" Feature


In this tutorial we will create Simple Facebook Time Ago using PHP. This code can compressed multiple files at the same time. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding.

Before we get 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_post, 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_post');
  3.  
  4. if(!$conn){
  5. die("Error: Can't connect to database!");
  6. }
  7. ?>

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" href="https://sourcecodester.com">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 - Simple Facebook Time Ago</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form>
  18. <div class="col-md-2"></div>
  19. <div class="col-md-8">
  20. <div class="form-group">
  21. <label>Name:</label>
  22. <input type="text" id="name" class="form-control"/>
  23. </div>
  24. <br />
  25. <div class="form-group">
  26. <label>What's on my mind...</label>
  27. <textarea id="content" class="form-control" style="height:200px;"></textarea>
  28. </div>
  29. <br />
  30. <center><button type="button" id="post" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Post</button></center>
  31. </div>
  32. </form>
  33. <br style = "clear:both;"/><br />
  34. <div id="result"></div>
  35. </div>
  36. </body>
  37. <script src="js/jquery-3.2.1.min.js"></script>
  38. <script src="js/bootstrap.js"></script>
  39. <script src="js/script.js"></script>
  40. </html>

Creating PHP Queries

This code contains the php query of the application. This code will save your data inputs to the database server with the help of ajax request, and display the data from the database to the page output. To do that just copy and write this block of codes inside the text editor, then save it as shown below.

save.php

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $name = $_POST['name'];
  5. $content = addslashes($_POST['content']);
  6. $time = date("Y-m-d H:i:s");
  7. $conn->query("INSERT INTO `post` VALUES('', '$name', '$content', '$time')");
  8. ?>

post.php

  1. <?php
  2. include 'time.php';
  3. require 'conn.php';
  4. $query = $conn->query("SELECT * FROM `post` ORDER BY `post_id` DESC");
  5. while($fetch = $query->fetch_array()){
  6. ?>
  7. <div class = "col-md-12 content" style = "word-wrap:break-word; background-color:#fff; padding:20px;">
  8. <h4><?php echo $fetch['name']?></h4>
  9. <hr style = "border-top:1px solid #000;"/>
  10. <?php echo $fetch['content']?>
  11. <br /><br />
  12. <h5 class="pull-right" style="color:#778899;"><?php echo Time_Convert($fetch['time'])?></h5>
  13. <br />
  14. </div>
  15. <br style = "clear:both;"/>
  16. <br />
  17. <?php
  18. }
  19. ?>

time.php

  1. <?php
  2. function Time_Convert($time){
  3. $time_in_seconds = strtotime($time);
  4. $current_time = time();
  5. $difference_in_seconds = $current_time - $time_in_seconds;
  6. $seconds = $difference_in_seconds;
  7. $minutes = round($seconds/60);
  8. $hours = round($seconds/3600);
  9. $days = round($seconds/86400);
  10. $weeks = round($seconds/604800);
  11. $months = round($seconds/2629440);
  12. $years = round($seconds/31553280);
  13.  
  14. if($seconds <= 60){
  15. return "Just Now";
  16.  
  17. }else if($minutes <= 60){
  18. if($minutes == 1){
  19. return "1 min";
  20. }else{
  21. return "$minutes mins";
  22. }
  23.  
  24. }else if($hours <=24){
  25. if($hours == 1){
  26. return "1 hr";
  27. }else{
  28. return "$hours hrs";
  29. }
  30. }else if($days <= 7){
  31. if($days == 1){
  32. return "yesterday";
  33. }else{
  34. return "$days days";
  35. }
  36. }else if($weeks <= 4.3){
  37. if($weeks == 1){
  38. return "a week";
  39. }else{
  40. return "$weeks weeks";
  41. }
  42. }else if($months <= 12){
  43. if($months == 1){
  44. return "1 month";
  45. }else{
  46. return "$months months";
  47. }
  48. }else{
  49. if($years == 1){
  50. return "1 yr";
  51. }else{
  52. return "$years yrs";
  53. }
  54. }
  55.  
  56. }
  57. ?>

Creating The jQuery Script

This is where the code that uses jquery script been used. This code will send ajax request to the database server, and then return some data to display in the page. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. $(document).ready(function(){
  2. DisplayData();
  3.  
  4. $('#post').on('click', function(){
  5. var name = $('#name').val();
  6. var content = $('#content').val();
  7. $.ajax({
  8. url: 'save.php',
  9. type: 'POST',
  10. data: {
  11. name: name,
  12. content: content
  13. },
  14. success: function(data){
  15. alert("Data Inserted");
  16. $('#name').val('');
  17. $('#content').val('');
  18. DisplayData();
  19. }
  20. });
  21. });
  22.  
  23. function DisplayData(){
  24. $.ajax({
  25. url: 'post.php',
  26. type: 'POST',
  27. data: {
  28. res: 1
  29. },
  30. success: function(data){
  31. $('#result').html(data);
  32. }
  33. });
  34. }
  35. });

There you have it we successfully created a Simple Facebook Time Ago 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!!!

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.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.