0

This code is taken from http://itfeast.blogspot.in/2013/08/php-convert-timestamp-into-facebook.html

How would you actually show this on a page? Say I have a mysql table with date column(timestamp) and I would like to insert the results of this code in there and as well as show it on page, how would you do it correctly?

There are far too many variables. I am trying to create 1 variable with all of them included and inserting it into db query. Don't know if it's correct.

$today = time('$createdday |  $datediff | $difftext | seconds | $minutes | $days | $hours | $months | $years');

.......Orginal code below.

<?php  
 $today = time();    
                 $createdday= strtotime($post['created']); //mysql timestamp of when post was created  
                 $datediff = abs($today - $createdday);  
                 $difftext="";  
                 $years = floor($datediff / (365*60*60*24));  
                 $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24));  
                 $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));  
                 $hours= floor($datediff/3600);  
                 $minutes= floor($datediff/60);  
                 $seconds= floor($datediff);  
                 //year checker  
                 if($difftext=="")  
                 {  
                   if($years>1)  
                    $difftext=$years." years ago";  
                   elseif($years==1)  
                    $difftext=$years." year ago";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($months>1)  
                    $difftext=$months." months ago";  
                    elseif($months==1)  
                    $difftext=$months." month ago";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($days>1)  
                    $difftext=$days." days ago";  
                    elseif($days==1)  
                    $difftext=$days." day ago";  
                 }  
                 //hour checker  
                 if($difftext=="")  
                 {  
                    if($hours>1)  
                    $difftext=$hours." hours ago";  
                    elseif($hours==1)  
                    $difftext=$hours." hour ago";  
                 }  
                 //minutes checker  
                 if($difftext=="")  
                 {  
                    if($minutes>1)  
                    $difftext=$minutes." minutes ago";  
                    elseif($minutes==1)  
                    $difftext=$minutes." minute ago";  
                 }  
                 //seconds checker  
                 if($difftext=="")  
                 {  
                    if($seconds>1)  
                    $difftext=$seconds." seconds ago";  
                    elseif($seconds==1)  
                    $difftext=$seconds." second ago";  
                 }  
                 echo " | ".$difftext;  
 ?>  

UPDATE I've got it working. Below is the new code.

a function to convert timestamp.

public static function facebook_date_format($timestamp) {
        $today = time();    
        $createdday= strtotime($timestamp); //mysql timestamp of when post was created  
        $datediff = abs($today - $createdday);  
        $difftext="";  
        $years = floor($datediff / (365*60*60*24));  
        $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24));  
        $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));  
        $hours= floor($datediff/3600);  
        $minutes= floor($datediff/60);  
        $seconds= floor($datediff);  
        //year checker  
        if($difftext=="")  
        {  
         if($years>1)  
          $difftext=$years." years ago";  
         elseif($years==1)  
          $difftext=$years." year ago";  
        }  
        //month checker  
        if($difftext=="")  
        {  
          if($months>1)  
          $difftext=$months." months ago";  
          elseif($months==1)  
          $difftext=$months." month ago";  
        }  
        //month checker  
        if($difftext=="")  
        {  
          if($days>1)  
          $difftext=$days." days ago";  
          elseif($days==1)  
          $difftext=$days." day ago";  
        }  
        //hour checker  
        if($difftext=="")  
        {  
          if($hours>1)  
          $difftext=$hours." hours ago";  
          elseif($hours==1)  
          $difftext=$hours." hour ago";  
        }  
        //minutes checker  
        if($difftext=="")  
        {  
          if($minutes>1)  
          $difftext=$minutes." minutes ago";  
          elseif($minutes==1)  
          $difftext=$minutes." minute ago";  
        }  
        //seconds checker  
        if($difftext=="")  
        {  
          if($seconds>1)  
          $difftext=$seconds." seconds ago";  
          elseif($seconds==1)  
          $difftext=$seconds." second ago";  
        }  
        return $difftext;  
    }

Apparently i was under the impression that I needed to insert the above code into the db. That is not the case. Basically insert your query into db as you normally do. For eg.

Insert data

$date = date('Y-m-d H:i:s');
$sth = $dbh->prepare("INSERT INTO posts (title, date) values ($title, $date)");

Get the data from db.

$get  =   $dbh->prepare("SELECT * FROM posts");
$get->execute();
$results = $get->fetch();
if (count($results) > 0) {
    foreach ($results as $row) {

         echo $row['title'] . '<br />';
         echo $row['date'] . '<br />';
    }
} else {
    echo "<p>Nothing matched your query.</p>";
}

This is where you use the facebook time function.

echo facebook_date_format($row['date']);

That's basically it. It does work 100%. Hope it helps. And I should mention someone else helped me with this code. I would like to thank that person and give him credit for it.

thatoneguy
  • 23
  • 1
  • 6
  • possible duplicate of [Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...](http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago) – John Conde Mar 28 '14 at 14:18
  • Looks to be. Anyways, I got it working. I'm good. – thatoneguy Mar 28 '14 at 19:57
  • If you find the answer yourself then you are supposed to give it to everyone. You expect everyone to give answers to you but you don't give answers to others. How does that work? Post the answer you found. You can answer your own questions, it is ok. Spreading knowledge is the 'name of the game'. – Ryan Vincent Mar 28 '14 at 23:45
  • Your right. I just updated my my original post. – thatoneguy Mar 29 '14 at 01:05

0 Answers0