Mar*_*rty 0 php time unix-timestamp
是否有一个PHP库将unix时间戳转换为这样的格式:
8 hours and 17 minutes ago
Run Code Online (Sandbox Code Playgroud)
我做了我自己的脚本,这样做,我只是无法追踪它,宁愿不花时间重新创建它(加上我认为它可以更有效地完成).
我原来的代码是这样的:
$seconds = time() - $timestamp;
$minutes = 0;
$hours = 0;
$days = 0;
$weeks = 0;
$months = 0;
$years = 0;
while($seconds >= 60)
{
$seconds -= 60;
$minutes ++;
if($minutes >= 60)
{
$minutes -= 60;
$hours ++;
if($hours >= 24)
{
// etc
}
}
}
if($hours < 1) return "$minutes minute" . ($minutes == 1 ? "" : "s")) . " and $seconds seconds" . ($seconds == 1 ? "" : "s"));
if($minutes < 1) return "$seconds second" . ($seconds == 1 ? "" : "s"));
// etc
Run Code Online (Sandbox Code Playgroud)
更好的是,如果有更好的方法来解决上述问题,我也会给自己一个破解.
是的,请检查DateTime类.
$datetime1= new DateTime();
$datetime2= new DateTime();
$datetime2->setTimestamp($timestamp);
$interval = $datetime2->diff($datetime1);
echo $interval->format('%a days %h hours and %i minutes ago');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1127 次 |
| 最近记录: |