I have timestamps in the following format: 2013-10-19 18:47:30
我有以下格式的时间戳:2013-10-19 18:47:30
I am using this to convert to relative (x minutes, hours, days ago) time but it returns nothing for the most recent users, I assume because my system time is GMT -3 hours so it is interpreting it as a time in the future. If that is the case, how can I take the users GMT offset into account in the result?
我使用它转换为相对(x分钟,小时,天前)时间,但它没有为最近的用户返回任何内容,我假设因为我的系统时间是格林威治标准时间-3小时所以它将它解释为未来的时间。如果是这种情况,我如何在结果中考虑用户GMT偏移?
$time = strtotime('2013-04-28 17:25:43');
echo 'event happened '.humanTiming($time).' ago';
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
2 个解决方案
#1
0
$eventTimeString = '2013-04-28 17:25:43';
$timezone = new DateTimeZone("Etc/GMT+3"); // Same as GMT-3, here you should use the users's timezone
$eventTime = new DateTime($eventTimeString , $timezone);
$diff = $eventTime->diff(new DateTime()); // Compare with
The $diff
variable now has number of seconds, months, days etc. since $eventTime
$ diff变量现在具有自$ eventTime以来的秒数,月数,天数等
object(DateInterval)#4 (15) {
["y"]=>
int(0)
["m"]=>
int(7)
["d"]=>
int(18)
["h"]=>
int(17)
["i"]=>
int(35)
["s"]=>
int(38)
["weekday"]=>
int(0)
["weekday_behavior"]=>
int(0)
["first_last_day_of"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
int(232)
["special_type"]=>
int(0)
["special_amount"]=>
int(0)
["have_weekday_relative"]=>
int(0)
["have_special_relative"]=>
int(0)
}
#2
0
Here is a rewrite of your method that will be more reliable/accurate:
这是对您的方法的重写,将更加可靠/准确:
function humanTiming($time){
$timezone=new DateTimeZone("Australia/Melbourne"); // declare whatever your user's timezone is
$time=new DateTime($time,$timezone); // input your datetime
$now=new DateTime("now",$timezone); // get current datetime
if($time>$now){
return "event hasn't happened yet";
}elseif($time==$now){
return "event is happening";
}
$diff=(array)$time->diff($now); // get time between now and $time, cast as array
$labels=array("y"=>"year","m"=>"month","d"=>"day","h"=>"hour","i"=>"minute","s"=>"second");
$readable=""; // declare as empty string
// filter the $diff array to only the desired elements and loop
foreach(array_intersect_key($diff,$labels) as $k=>$v){
if($v>0){ // only add non-zero values to $readable
$readable.=($readable!=""?", ":"")."$v {$labels[$k]}".($v>1?"s":"");
// use comma-space as glue | show value | show unit | pluralize when necessary
}
}
return "event happened $readable ago";
}
echo humanTiming('2013-04-28 17:25:43');
// event happened 3 years, 10 months, 23 days, 8 hours, 33 minutes, 59 seconds ago
#1
0
$eventTimeString = '2013-04-28 17:25:43';
$timezone = new DateTimeZone("Etc/GMT+3"); // Same as GMT-3, here you should use the users's timezone
$eventTime = new DateTime($eventTimeString , $timezone);
$diff = $eventTime->diff(new DateTime()); // Compare with
The $diff
variable now has number of seconds, months, days etc. since $eventTime
$ diff变量现在具有自$ eventTime以来的秒数,月数,天数等
object(DateInterval)#4 (15) {
["y"]=>
int(0)
["m"]=>
int(7)
["d"]=>
int(18)
["h"]=>
int(17)
["i"]=>
int(35)
["s"]=>
int(38)
["weekday"]=>
int(0)
["weekday_behavior"]=>
int(0)
["first_last_day_of"]=>
int(0)
["invert"]=>
int(0)
["days"]=>
int(232)
["special_type"]=>
int(0)
["special_amount"]=>
int(0)
["have_weekday_relative"]=>
int(0)
["have_special_relative"]=>
int(0)
}
#2
0
Here is a rewrite of your method that will be more reliable/accurate:
这是对您的方法的重写,将更加可靠/准确:
function humanTiming($time){
$timezone=new DateTimeZone("Australia/Melbourne"); // declare whatever your user's timezone is
$time=new DateTime($time,$timezone); // input your datetime
$now=new DateTime("now",$timezone); // get current datetime
if($time>$now){
return "event hasn't happened yet";
}elseif($time==$now){
return "event is happening";
}
$diff=(array)$time->diff($now); // get time between now and $time, cast as array
$labels=array("y"=>"year","m"=>"month","d"=>"day","h"=>"hour","i"=>"minute","s"=>"second");
$readable=""; // declare as empty string
// filter the $diff array to only the desired elements and loop
foreach(array_intersect_key($diff,$labels) as $k=>$v){
if($v>0){ // only add non-zero values to $readable
$readable.=($readable!=""?", ":"")."$v {$labels[$k]}".($v>1?"s":"");
// use comma-space as glue | show value | show unit | pluralize when necessary
}
}
return "event happened $readable ago";
}
echo humanTiming('2013-04-28 17:25:43');
// event happened 3 years, 10 months, 23 days, 8 hours, 33 minutes, 59 seconds ago