你会如何执行这个时间戳代码?

时间:2014-03-28 14:15:40

标签: php mysql datetime timestamp days

此代码取自http://itfeast.blogspot.in/2013/08/php-convert-timestamp-into-facebook.html

你如何在页面上显示这个?假设我有一个带有日期列(时间戳)的mysql表,我想在此处插入此代码的结果,并在页面上显示,您将如何正确地执行此操作?

变量太多了。我正在尝试创建包含所有这些变量的1变量并将其插入到db查询中。不知道它是否正确。

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

.......下面的原始代码。

<?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;  
 ?>  

更新 我已经开始工作了。以下是新代码。

转换时间戳的函数。

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;  
    }

显然我的印象是我需要将上面的代码插入到数据库中。事实并非如此。基本上像往常一样将查询插入到db中。例如。

插入数据

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

从数据库中获取数据

$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>";
}

这是您使用脸书时间功能的地方。

echo facebook_date_format($ row [&#39; date&#39;]);

基本上就是这样。它确实100%工作。希望能帮助到你。 我应该提到其他人帮助我使用此代码。我要感谢那个人并给予他信任。

0 个答案:

没有答案