Time Since Post

Source code available for selected videos from phpacademy YouTube channel.

Time Since Post

Postby alex » Thu Feb 11, 2010 12:21 am

For http://www.youtube.com/watch?v=0f4F7SE0OzQ

The form submission code is below. This doesn't include the tutorial feature and allows you to follow along using what I already had created.

  1. <?php
  2. // connect
  3. $connect_error = "Connection error";
  4. mysql_connect("localhost", "root", "") or die($connect_error);
  5. mysql_select_db("timesincepost") or die($connect_error);
  6.  
  7. $posted = $_POST['posted'];
  8. $text = addslashes($_POST['text']); // addslashes() function for query security
  9. $time = time();
  10.  
  11. if ($posted=="true")
  12. {
  13.    $insert_error = "Your text could not be posted";
  14.    $insert = mysql_query("INSERT INTO posts VALUES ('','$time','$text')") or die($insert_error);
  15. }
  16.  
  17. ?>
  18.  
  19. <html>
  20. <body>
  21. <p />
  22. <form action="index.php" method="POST">
  23. Text:<br />
  24. <textarea name="text"></textarea><br />
  25. <input type="hidden" name="posted" value="true">
  26. <input type="submit" value="Post">
  27. </form>
  28.  
  29. </body>
  30. </html>
  31.  
  32. <?php
  33.  
  34.  
  35. ?>


And the code below is the full source code (use it to LEARN!)

  1. <?php
  2. // connect
  3. $connect_error = "Connection error";
  4. mysql_connect("localhost", "root", "") or die($connect_error);
  5. mysql_select_db("timesincepost") or die($connect_error);
  6.  
  7. $posted = $_POST['posted'];
  8. $text = addslashes($_POST['text']); // addslashes() function for query security
  9. $time = time();
  10.  
  11. if ($posted=="true")
  12. {
  13.    $insert_error = "Your text could not be posted";
  14.    $insert = mysql_query("INSERT INTO posts VALUES ('','$time','$text')") or die($insert_error);
  15. }
  16.  
  17. ?>
  18.  
  19. <html>
  20. <body>
  21. <p />
  22. <form action="index.php" method="POST">
  23. Text:<br />
  24. <textarea name="text"></textarea><br />
  25. <input type="hidden" name="posted" value="true">
  26. <input type="submit" value="Post">
  27. </form>
  28.  
  29. </body>
  30. </html>
  31.  
  32. <?php
  33.  
  34. $get = mysql_query("SELECT * FROM posts ORDER BY time DESC");
  35. while ($get_row = mysql_fetch_assoc($get))
  36. {
  37.       // data
  38.       $get_time = $get_row['time'];
  39.       $get_text = $get_row['text'];
  40.      
  41.       $diff = $time - $get_time; // seconds
  42.      
  43.       switch(1)
  44.       {
  45.          case ($diff < 60):
  46.          $count = $diff;
  47.          if ($count==0)
  48.             $count = "a moment";
  49.          else if ($count==1)
  50.             $suffix = "second";
  51.          else
  52.             $suffix = "seconds";
  53.          break;
  54.          
  55.          case ($diff > 60 && $diff < 3600):
  56.          $count = floor($diff/60);
  57.          if ($count==1)
  58.             $suffix = "minute";
  59.          else
  60.             $suffix = "minutes";
  61.          break;
  62.  
  63.          case ($diff > 3600 && $diff < 86400):
  64.          $count = floor($diff/3600);
  65.          if ($count==1)
  66.             $suffix = "hour";
  67.          else
  68.             $suffix = "hours";
  69.          break;
  70.          
  71.          case ($diff > 86400 && $diff < 604800):
  72.          $count = floor($diff/86400);
  73.          if ($count==1)
  74.             $suffix = "day";
  75.          else
  76.             $suffix = "days";
  77.          break;
  78.          
  79.          case ($diff > 604800 && $diff < 2629743):
  80.          $count = floor($diff/604800);
  81.          if ($count==1)
  82.             $suffix = "week";
  83.          else
  84.             $suffix = "weeks";
  85.          break;
  86.          
  87.          case ($diff > 2629743 && $diff < 31556926):
  88.          $count = floor($diff/2629743);
  89.          if ($count==1)
  90.             $suffix = "month";
  91.          else
  92.             $suffix = "months";
  93.          break;
  94.          
  95.          case ($diff > 31556926):
  96.          $count = floor($diff/31556926);
  97.          if ($count==1)
  98.             $suffix = "year";
  99.          else
  100.             $suffix = "years";
  101.          break;
  102.  
  103.       }
  104.      
  105.       echo $get_text."<br /> Posted ".$count." ".$suffix." ago <p />";
  106.  
  107. }
  108.  
  109. ?>
User avatar
alex
Founder
 
Posts: 483
Joined: Mon Apr 20, 2009 9:55 pm
Location: London, UK.
Online: 4d 11h 32m 26s
Karma: 11

Re: Time Since Post

Advertisment

Advertisment
 

Return to Source code

Who's online?

Users browsing this forum: No registered users and 1 guest