gpt4 book ai didi

PHP Facebook 风格的日期

转载 作者:可可西里 更新时间:2023-10-31 23:46:11 25 4
gpt4 key购买 nike

我为我的博客创建了一个 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'];
}
}

最佳答案

在您的 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 秒内。

关于PHP Facebook 风格的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35678631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com