我正在尝试transform a unix timestamp into a human readable string so i can show how long ago a user signed up
。
这是我的数据:
mysql> select createdate as unixtimestamp,date_format(from_unixtime(createdate),'%e %b %Y') as dateformatted from users where userid=40645;
+---------------+---------------+
| unixtimestamp | dateformatted |
+---------------+---------------+
| 1162642968 | 4 Nov 2006 |
+---------------+---------------+
1 row in set (0.00 sec)
mysql>
好的,这就是问题所在。我在互联网上发现了3个不同的函数,它们从unix时间戳返回一个人类可读的字符串。 所有3个都无法正常工作。
我希望有人查看这些功能,并帮助我弄清楚如何修复其中一个以返回正确的人类可读字符串。
开始演出!
这是功能#1:
function getElapstedTimeHumanReadable($time)
{
$names = array("seconds", "minutes", "hours", "days", "months", "years");
$values = array(1, 60, 3600, 24 * 3600, 30 * 24 * 3600, 365 * 24 * 3600);
$time = time()-$time;
for($i = count($values) - 1; $i > 0 && $time < $values[$i]; $i--);
if($i == 0) {
$timestamp = intval($time / $values[$i]) . " " . $names[$i];
} else {
$t1 = intval($time / $values[$i]);
$t2 = intval(($time - $t1 * $values[$i]) / $values[$i - 1]);
$timestamp= "$t1 " . $names[$i] . ", $t2 " . $names[$i - 1];
}
return $timestamp;
}
此功能的返回值是“加入1天,17小时前”
显然这不正确。
这是功能#2:
function getElapsedTimeHumanReadable($time)
{
$time = time() - $time;
$points = array(
'year' => 31556926,
'month' => 2629743,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1
);
foreach($points as $point => $value)
{
if($elapsed = floor($time/$value) > 0)
{
$s = $elapsed>1?'s':'';
$timestamp = "$elapsed $point$s";
break;
}
}
return $timestamp;
}
此功能的返回值为“加入1天前
”最后,这是函数#3:
function getElapsedTimeHumanReadable($time)
{
$etime=time()-$time;
if ($etime < 1)
{
return '0 seconds';
}
$a = array( 365 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
$a_plural = array( 'year' => 'years',
'month' => 'months',
'day' => 'days',
'hour' => 'hours',
'minute' => 'minutes',
'second' => 'seconds'
);
foreach ($a as $secs => $str)
{
$d = $etime / $secs;
if ($d >= 1)
{
$r = round($d);
return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
}
}
}
所以我的代码和我的数据。 不太确定为什么似乎没有工作。我试着查看代码,但我无法弄清楚如何解决它。
Whats interesting is they all say 2 days, but my timestamp appears to show 2006.
感谢您的帮助。
答案 0 :(得分:1)
$time = 1162642968 ;
$date = new DateTime( );
$date->setTimestamp( $time );
$today = new DateTime( 'now', new DateTimeZone( "Europe/Rome" ) );
$diff = $today->diff( $date);
echo "Year: " . $diff->y . " - Month: " . $diff->m . " - Days: " . $diff->d . " - Hours: " . $diff->h;
正如所建议的那样,我会添加解释,即使我认为这是自我解释。
$ date = new DateTime()创建对象,$ date-&gt; setTimestamp($ time)用于将该日期放在mysql时间戳的值中。
$ today创建指向实际日期。
$ date-&gt; diff()创建一个包含所有必要数据的DateInterval对象(http://php.net/manual/en/class.dateinterval.php)。
答案 1 :(得分:0)
如果您想自己解决这个问题,您应该计算差异并将其基于这些值。我还没有测试过RiccardoC的答案,但这似乎是一个不错的选择。
正如我在你的帖子中看到的,你计算的一年总是365天,所以如果你不想详细介绍时区,额外的时间,额外的天数,不同的月份长度等等,那么你可以使用简单的东西:
function getElapsedTimeHumanReadable($timestamp) {
$diff = time() - $timestamp;
$years = intval($diff/31536000); //seconds in a year 60*60*24*365
$diff -= ($years*31536000);
$months = intval($diff/2592000); //seconds in a month 60*60*24*30
$diff -= ($months*2592000);
$days = intval($diff/86400); //seconds in a day 60*60*24
return $years." years, ".$months." months, ".$days." days ago";
}
echo getElapsedTimeHumanReadable(1162642968); // November 4th, 2006
回声9 years, 0 months, 17 days ago