Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将Unix时间戳转换为";“x秒前”;_Php_Unix - Fatal编程技术网

Php 将Unix时间戳转换为";“x秒前”;

Php 将Unix时间戳转换为";“x秒前”;,php,unix,Php,Unix,因此,我有一个表,其中列出了我的游戏服务器中的聊天信息(聊天信息存储在数据库中),我有一个UNIX timestap,例如1455749769 有人知道我如何使用php转换时间戳,使其回显聊天消息的时间,例如:“5秒前” 这是我的桌子 $name=$row['client_name']; $time=$row['msg_time']; $name=htmlentities($name); 回声“; 回显“$time”; 回声“; 回显“$msg”; 回声“; } 回声“; }否则{ 回显“0结

因此,我有一个表,其中列出了我的游戏服务器中的聊天信息(聊天信息存储在数据库中),我有一个UNIX timestap,例如1455749769

有人知道我如何使用php转换时间戳,使其回显聊天消息的时间,例如:“5秒前”

这是我的桌子

$name=$row['client_name'];
$time=$row['msg_time'];
$name=htmlentities($name);
回声“;
回显“$time”;
回声“;
回显“$msg”;
回声“;
}
回声“;
}否则{
回显“0结果”;
回声“;
}
$conn->close();

?>
时间戳是从历元开始的秒数,因此只需获取当前时间戳并减去:

$seconds = time() - $time

只需获取当前时间并减去:

$now = time();
//results into an unix like 1455750460
//then just substract:

$diff = $now - $time
// gives you the passed seconds

//readable
echo date('H:i:s', $diff);

我已经问过这个问题了,在问之前一定要搜索是的,但在这个问题上,他的数据库格式是2016-01-21 23:15:00,而我的是1455749769Touché,这是你的观点:谢谢你,第二个问题,你知道我怎样才能让它像1m 5s前、1h 1m 5s前和1d 1h 1m 5s前那样运行吗?:)我编辑了上面的示例。看看这里:谢谢你,第二个问题,你知道我如何让它像1m 5s前、1h 1m 5s前和1d 1h 1m 5s前那样运行吗?:)@JonnyMackell:看看这个答案: