我如何使用 php time() 获取用户的本地时间,或者还有其他方法可以做到这一点?这意味着它将在他们的时区。
我需要用用户的当地时间计算前时间,这是我为前时间函数找到的一个不错的脚本:
<?php
function timeago($time)
{
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$difference = $now - $time;
$tense = "ago";
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++)
{
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1)
{
$periods[$j].= "s";
}
return "$difference $periods[$j] $tense";
}
?>
<?php
$date = strtotime('2012-04-15 07:41:03');
$datep = timeago($date);
echo $datep;
?>
从上面的脚本中,我使用 time() 但它只获取我的服务器时间而不是我身边的本地时间。我怎样才能得到我的当地时间(用户的当地时间)?