PHP Facebook风格的日期
发布时间:2020-12-13 22:25:44 所属栏目:PHP教程 来源:网络整理
导读:我为我的博客创建了一个 PHP脚本.我正在尝试将UNIX时间戳转换为像Facebook一样显示.例如,当您在Twitter Facebook上看到帖子或评论时,您会看到日期/时间显示为“10分钟前”或“20天前”或“1周前”. 我找到了一个PHP脚本,但我无法添加我的脚本.你能帮我吗? ?
我为我的博客创建了一个
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秒内. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |