PHP 程序将给定时间戳转换为过去时间
要将给定时间戳转换为过去时间,代码如下所示 -
示例
<?php
function to_time_ago( $time )
{
$difference = time() - $time;
if( $difference < 1 )
{
return 'less than only a second ago';
}
$time_rule = array (
12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach( $time_rule as $sec => $my_str )
{
$res = $difference / $sec;
if( $res >= 1 )
{
$t = round( $res );
return $t . ' ' . $my_str .
( $t > 1 ? 's' : '' ) . ' ago';
}
}
}
echo "The timestamp to time ago conversion is ";
echo to_time_ago( time() - 600);
?>输出
The timestamp to time ago conversion is 10 minutes ago
定义了一个名为“to_time_ago”的函数,用于检查传递给函数作为参数的时间和时间函数之间的差异。如果发现此差异小于 1,它表示时间仅从一秒前过去。否则,将在数组中生成年、月、日、时、分和秒。使用“foreach”循环迭代之前生成的数组。计算时间差并将其打印在控制台上。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP