PHP Facebook风格的日期

时间:2016-02-28 04:19:08

标签: php facebook date

我为我的博客创建了一个PHP脚本。我试图将UNIX时间戳转换为像Facebook一样显示。例如,当您在Twitter Facebook上看到帖子或评论时,您会看到日期/时间显示为" 10分钟前"或" 20天前"或" 1周前"。

我找到了一个PHP脚本,但我无法在脚本中添加。你能帮帮我吗?

<?php
//Function for inserting post
function insertPost()
{
    if (isset($_POST['sub'])) {
        global $con;
        global $user_id;
        $title = $_POST['title'];
        $content = $_POST['content'];
        $topic = $_POST['topic'];
        $adfile = $_POST['post_file'];
        $insert = "insert into posts (user_id,topic_id,post_title,post_content,post_date,post_file) values ('$user_id','$topic','$title','$content',NOW(),'$adfile')";
        $run = mysqli_query($con, $insert);
        if ($run) {
            echo "This is a test.";
            $update = "update users set posts='yes' where user_id='$user_id'";
            $run_update = mysqli_query($con, $update);
        }
    }
}

//Function for displaying posts
function get_posts()
{
    global $con;
    $per_page = 20;// page siralama buradan basliyor
    if (isset($_GET['page'])) {
        $page = $_GET['page'];
    } else {
        $page = 1;
    }
    $start_from = ($page - 1) * $per_page; //Buradan yukarisi bu alanda dahil page siralama icin SILINECEK!!!
    $get_posts = "select * from posts ORDER by 1 DESC LIMIT $start_from,$per_page";
    $run_posts = mysqli_query($con, $get_posts);
    while ($row_posts = mysqli_fetch_array($run_posts)) {
        $post_id = $row_posts['post_id'];
        $user_id = $row_posts['user_id'];
        $post_title = $row_posts['post_title'];
        $content = $row_posts['post_content'];
        $post_date = $row_posts['post_date'];
        $adfile = $row_posts['post_file'];
        /***getting the user who has posted the thread**/
        $user = "select * from users where user_id='$user_id' AND posts='yes'";
        $run_user = mysqli_query($con, $user);
        $row_user = mysqli_fetch_array($run_user);
        $user_name = $row_user['user_name'];
        $user_image = $row_user['user_image'];
        $user = $_SESSION['user_email'];
        $get_user = "select * from users where user_email='$user'";
        $run_user = mysqli_query($con, $get_user);
        $row = mysqli_fetch_array($run_user);
        $user_country = $row['user_country'];
    }
}

2 个答案:

答案 0 :(得分:0)

在您的get_posts()函数中,您有:

$post_date = $row_posts['post_date'];

您要做的是将其转换为原始时间值,并将今天转换为原始时间值。然后减去两个原始值,然后将结果除以60.所以在上面的语句中,你可以添加这些行:

$post_timestamp = strtotime($post_date);
$now_timestamp = time();
$seconds_ago = $now_timestamp - $post_timestamp;
$minutes_ago = intval($seconds_ago / 60);

然后您可以在函数的其他位置使用$minutes_ago变量。例如,您可以在上面的行下面添加此行:

echo "This item was added ".$minutes_ago." minute(s) ago.";

如果您想要精确计时,请从我的代码中删除intval

**更新**

在回复评论时,您必须应用数学来确定要使用的后缀。

所以请$seconds_ago创建条件:

$div_minute=60; //raw value for minute = 60 seconds
$div_hour=$div_minute*60; //raw value for hour = 60 * 60 = 3600 seconds
$div_day=$div_hour*24; //raw value for day = 60 * 60 * 24 = 86400 seconds
$div_week=$div_day*7; //raw value for week = 60 * 60 * 24 * 7 seconds

if ($seconds_ago >= $div_week){
    echo "Updated about ".intval($seconds_ago/$div_week)." week(s) ago";
}else{
    if ($seconds_ago >= $div_day){
        echo "Updated about ".intval($seconds_ago/$div_day)." days(s) ago";
    }else{
        if ($seconds_ago >= $div_hour){
            echo "Updated about ".intval($seconds_ago/$div_hour)." hours(s) ago";
        }else{
            if ($seconds_ago > 10){ // Does "now" in your app mean within the last 10 seconds?
                echo "Updated ".$seconds_ago." second(s) ago.";
            }else{
                echo "Updated just now";
            }
        }
   }
}

在我的代码中,我认为“现在”意味着在最后10秒内。

答案 1 :(得分:0)

我知道您要求使用PHP,但处理此问题的库可以免费使用,并称为moment.js。如果您的博客有一个javascript图层,那么在客户端进行此字符串转换可能会更容易。请参阅http://momentjs.com/docs/#/displaying/from/