Sahitya-K

Seconds ago php

Apr 1st, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>test1</title>
  5. </head>
  6. <body>
  7. <form method="POST">
  8.     <input type="text" name="title" placeholder="Enter Your Heading....." required><br><br>
  9.     <textarea id="textarea" name="content" minlength="5" placeholder="Enter Your Content Here...." required></textarea><br><br><div id="textarea_feedback"></div>
  10.     <input type="text" name="tags" placeholder="Enter Your Tags" required><br><br>
  11.     <input type="submit" name="submit" value="Post"><br><br><br>
  12.     <script>
  13.         $(document).ready(function() {
  14.     $('#textarea').keyup(function() {
  15.         var text_length = $('#textarea').val().length;
  16.         $('#textarea_feedback').html(text_length + ' characters ');
  17.     });
  18. });
  19.     </script>
  20. </form>
  21. <?php
  22. $servername = "localhost";
  23. $username = "root";
  24. $password = "";
  25. $dbname = "izaya";
  26. $conn = new mysqli($servername, $username, $password, $dbname);
  27.     function timeago($time_ago)
  28.     {
  29.         $cur_time = time();
  30.         $time_elapsed = $cur_time - $time_ago;
  31.         $sec = $time_elapsed;
  32.         $min = round($time_elapsed / 60);
  33.         $hours = round($time_elapsed / 3600);
  34.         $days = round($time_elapsed / 86400);
  35.         $weeks = round($time_elapsed / 604800);
  36.         $months = round($time_elapsed / 2600640);
  37.         $years = round($time_elapsed / 31207680);
  38.  
  39.         if ($sec <= 60) {
  40.             echo "$sec Seconds ago";
  41.         }
  42.         else if ($min <= 60) {
  43.             if ($min == 1){
  44.                 echo "one minute ago";
  45.             }else {
  46.                 echo "$min Minutes ago";
  47.             }
  48.         }
  49.         else if ($hours <= 24) {
  50.             if ($hours == 1) {
  51.             echo "one hour ago";
  52.             }else {
  53.                 echo "$hours Hours ago";
  54.             }
  55.         }
  56.             else if ($days <= 7) {
  57.             if ($days == 1) {
  58.             echo "yesterday";
  59.             }else {
  60.                 echo "$days Days ago";
  61.             }
  62.         }
  63.             else if ($weeks <= 4.3) {
  64.             if ($weeks == 1) {
  65.             echo "one week ago";
  66.             }else {
  67.                 echo "$weeks weeks ago";
  68.             }
  69.         }
  70.             else if ($months <= 12) {
  71.             if ($months == 1) {
  72.             echo "one month ago";
  73.             }else {
  74.                 echo "$months Months ago";
  75.             }
  76.         }
  77.             else
  78.             if ($years == 1) {
  79.             echo "one Year ago";
  80.             }else {
  81.                 echo "$years Years ago";
  82.             }
  83.         }
  84.     $dat = date("Y-m-d H:i:s");
  85.     $query = "INSERT INTO posts (title,content,tags, tim) VALUES (?,?,?,?)";
  86.     if ($statment = $conn->prepare($query)) {
  87.         $statment->bind_param("ssss" ,  $_POST['title'], $_POST['content'],$_POST['tags'], $tim = $dat);
  88.         if ($statment->execute()) {
  89.             $take = $conn->query("SELECT title,content,tim FROM posts ORDER BY id");
  90.             $count = $take->num_rows;
  91.             if ($count) {
  92.                 while ($print = $take->fetch_assoc()) {
  93.                     echo '<br>';
  94.                     echo $print['title'];
  95.                     echo "<br>";
  96.                     echo $print['content'];
  97.                     echo "<br>";
  98.                     echo $print['tags'];
  99.                     echo "<br>";
  100.                     $waktu = $print['tim'];
  101.                     $time_ago = strtotime($waktu);
  102.                     echo timeago($time_ago);
  103.                 }
  104.         }
  105.             else {echo "something";}
  106.         }
  107.     }
  108.  
  109. else {
  110.     echo "string";
  111. }
  112. ?>
  113. </body>
  114. </html>
Add Comment
Please, Sign In to add comment