我需要以某种方式获取unix时间戳并将其输出如下
可以用MySQL完成吗?或者php
Mike 7s ago
Jim 44s ago
John 59s ago
Amanda 1m ago
Ryan 1m ago
Sarah 1m ago
Tom 2m ago
Pamela 2m ago
Ruben 3m ago
Pamela 5h ago
你可以猜到我只想打印分钟,而不是分钟和秒(1分3秒前)
我应该研究什么?
答案 0 :(得分:7)
是的,可以做到。 See related post
$before // this is a UNIX timestamp from some time in the past, maybe loaded from mysql
$now = time()
$diff = $now - $before;
if( 1 > $diff ){
exit('Target Event Already Passed (or is passing this very instant)');
} else {
$w = $diff / 86400 / 7;
$d = $diff / 86400 % 7;
$h = $diff / 3600 % 24;
$m = $diff / 60 % 60;
$s = $diff % 60;
return "{$w} weeks, {$d} days, {$h} hours, {$m} minutes and {$s} secs away!"
}
答案 1 :(得分:6)
PHP 5.3及更新版本具有DateTime
个对象,您可以使用从数据库返回的数据构建这些对象。这些DateTime
个对象使用diff
方法将两个日期之间的差异作为DateInterval
对象,然后您可以format
。
修改:将sub
更正为diff
。
编辑2: 通过这种方式实现两次捕获:
DateTime
的构造函数似乎不带UNIX时间戳...除非以@为前缀,如下所示:$startDate = new DateTime('@' . $timestamp);
$years = $dateDiff->format('y');
答案 2 :(得分:1)
function sECONDS_TO_DHMS($seconds)
{
$days = floor($seconds/86400);
$hrs = floor($seconds / 3600);
$mins = intval(($seconds / 60) % 60);
$sec = intval($seconds % 60);
if($days>0){
//echo $days;exit;
$hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
$hours = $hrs-($days*24);
$return_days = $days." Days ";
$hrs = str_pad($hours,2,'0',STR_PAD_LEFT);
}else{
$return_days="";
$hrs = str_pad($hrs,2,'0',STR_PAD_LEFT);
}
$mins = str_pad($mins,2,'0',STR_PAD_LEFT);
$sec = str_pad($sec,2,'0',STR_PAD_LEFT);
return $return_days.$hrs.":".$mins.":".$sec;
}
echo sECONDS_TO_DHMS(2); // Output 00:00:02
echo sECONDS_TO_DHMS(96000); // Output 1 Days 02:40:00
答案 3 :(得分:0)
答案 4 :(得分:0)
答案 5 :(得分:0)