像facebook消息系统一样计算时差[重复]
Posted
技术标签:
【中文标题】像facebook消息系统一样计算时差[重复]【英文标题】:calculate time difference like facebook messaging system [duplicate] 【发布时间】:2012-07-17 09:58:37 【问题描述】:我需要使用 php 在 facebook 消息中查找 like
的时差。
like: 2 weeks ago, 2 hr 30 mins ago, one second ago
我的时间格式是“Y-m-d H:i:s”
谁能帮帮我?
【问题讨论】:
我只能用 dayz 找到它,不能用小时分钟和秒找到它。 【参考方案1】:将消息创建时间存储在数据库中,然后使用以下函数:
function get_time_difference_php($created_time)
date_default_timezone_set('Asia/Calcutta'); //Change as per your default time
$str = strtotime($created_time);
$today = strtotime(date('Y-m-d H:i:s'));
// It returns the time difference in Seconds...
$time_differnce = $today-$str;
// To Calculate the time difference in Years...
$years = 60*60*24*365;
// To Calculate the time difference in Months...
$months = 60*60*24*30;
// To Calculate the time difference in Days...
$days = 60*60*24;
// To Calculate the time difference in Hours...
$hours = 60*60;
// To Calculate the time difference in Minutes...
$minutes = 60;
if(intval($time_differnce/$years) > 1)
return intval($time_differnce/$years)." years ago";
else if(intval($time_differnce/$years) > 0)
return intval($time_differnce/$years)." year ago";
else if(intval($time_differnce/$months) > 1)
return intval($time_differnce/$months)." months ago";
else if(intval(($time_differnce/$months)) > 0)
return intval(($time_differnce/$months))." month ago";
else if(intval(($time_differnce/$days)) > 1)
return intval(($time_differnce/$days))." days ago";
else if (intval(($time_differnce/$days)) > 0)
return intval(($time_differnce/$days))." day ago";
else if (intval(($time_differnce/$hours)) > 1)
return intval(($time_differnce/$hours))." hours ago";
else if (intval(($time_differnce/$hours)) > 0)
return intval(($time_differnce/$hours))." hour ago";
else if (intval(($time_differnce/$minutes)) > 1)
return intval(($time_differnce/$minutes))." minutes ago";
else if (intval(($time_differnce/$minutes)) > 0)
return intval(($time_differnce/$minutes))." minute ago";
else if (intval(($time_differnce)) > 1)
return intval(($time_differnce))." seconds ago";
else
return "few seconds ago";
【讨论】:
感谢您的帮助。这个脚本对我很有帮助。以上是关于像facebook消息系统一样计算时差[重复]的主要内容,如果未能解决你的问题,请参考以下文章
创建一个像 facebook 和 gmail 这样的线程私人消息系统
如何让来自 Firebase 控制台的通知像 Facebook 和其他应用一样弹出?