我为我的博客创建了一个 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/
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998
是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
我正在尝试使用在我的代码中是动态的Time.local来安排时间。在每个月的第一天,我传递的值是Time.local(2009,9,-1,0)。在PHP中,这会将时间设置为上个月的最后一天。在ruby中,我只是得到“ArgumentError:参数超出范围”。是我用错了方法还是什么?谢谢。 最佳答案 您应该使用DateTime类而不是Time。(您可能需要先require'date'并安装activesupportgem。)它比Time更通用,并且可以用DateTime.civil(2009,9-1,-1,0)做你想做的事。为天