0

我有一个 MySql 日期时间值,例如“2012-04-17 20:48:29”。我想将其转换为简单的文本,例如“10 天前”。我想在 php 或 javascript 中执行此操作!我试图创建自己的算法来做到这一点。但是是否有一个已经可用的解决方案来做到这一点?

4

4 回答 4

1

你可以使用这个模式

$date = "2012-04-17 20:48:29";

$seconds = time() - strtotime($date);
$days = floor($seconds / 86400);
$seconds -= $days * 86400;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;

echo "$days days, $hours hours, $minutes minutes, $seconds seconds ago";

您当然应该在回显结果之前添加一些条件。仅显示 1 分钟前、3 小时前或 10 天前...

于 2012-04-17T22:08:53.937 回答
1

使用此功能,您将获得如下输出:

  • 1分钟
  • 5分钟
  • 15小时
  • 4天
  • 2个月
  • 1.5年

function time_ago_in_words($time) {
  $from_time = strtotime($time);

  $to_time = strtotime(gmd());
  $distance_in_minutes = round((($to_time - $from_time))/60);

  if ($distance_in_minutes < 0)
    return (string)$distance_in_minutes.'E';

  if (between($distance_in_minutes, 0, 1))
    return '1 minute';

  elseif (between($distance_in_minutes, 2, 44))
    return $distance_in_minutes.' minutes';

  elseif (between($distance_in_minutes, 45, 89))
    return '1 hour';

  elseif (between($distance_in_minutes, 90, 1439))
    return round($distance_in_minutes/60).' hours';

  elseif (between($distance_in_minutes, 1440, 2879))
    return '1 day';

  elseif (between($distance_in_minutes, 2880, 43199))
    return round($distance_in_minutes/1440).' days';

  elseif (between($distance_in_minutes, 43200, 86399))
    return '1 month';

  elseif (between($distance_in_minutes, 86400, 525959))
    return round($distance_in_minutes/43200).' months';

  elseif ($distance_in_minutes > 525959)
    return number_format(round(($distance_in_minutes/525960), 1), 1).' years';
}

所以你可以这样做:

// Last time you logged in: 15 days ago.
Last time you logged in: <?php echo time_ago_in_words($user['last_logged_in']) ?> ago.
// We haven't seen you for 15 days!
We haven't seen you for <?php echo time_ago_in_words($user['last_logged_in']) ?>!
于 2012-04-17T22:54:08.033 回答
0

使用 PHP,您可以调用strftime以获得不同的输出。在这里查看更多详细信息。

您还可以查看一个 jQuery 插件:jQuery-dateFormat

玩得开心!

于 2012-04-17T22:08:10.770 回答
0

我使用这个功能:

function duration($integer)
 { 

     $seconds=$integer; 
     $minutes = 0;
     $hours = 0;
     $days = 0;
     $weeks = 0;
     $return = "";
     if ($seconds/60 >=1) 

     { 

     $minutes=floor($seconds/60); 

     if ($minutes/60 >= 1) 

     { # Hours 

     $hours=floor($minutes/60); 

     if ($hours/24 >= 1) 

     { #days 

     $days=floor($hours/24); 

     if ($days/7 >=1) 

     { #weeks 

     $weeks=floor($days/7); 

     if ($weeks>=2) $return="$weeks Weeks"; 

     else $return="$weeks Week"; 

     } #end of weeks 

     $days=$days-(floor($days/7))*7; 

     if ($weeks>=1 && $days >=1) $return="$return, "; 

     if ($days >=2) $return="$return $days days";

     if ($days ==1) $return="$return $days day";

     } #end of days

     $hours=$hours-(floor($hours/24))*24; 

     if ($days>=1 && $hours >=1) $return="$return, "; 

     if ($hours >=2) $return="$return $hours hours";

     if ($hours ==1) $return="$return $hours hour";

     } #end of Hours

     $minutes=$minutes-(floor($minutes/60))*60; 

     if ($hours>=1 && $minutes >=1) $return="$return, "; 

     if ($minutes >=2) $return="$return $minutes minutes";

     if ($minutes ==1) $return="$return $minutes minute";

     } #end of minutes 

     $seconds=$integer-(floor($integer/60))*60; 

     if ($minutes>=1 && $seconds >=1) $return="$return, "; 

     if ($seconds >=2) $return="$return $seconds seconds";

     if ($seconds ==1) $return="$return $seconds second";

     $return="$return."; 

     return $return; 

 }

echo duration(time() - strtotime($date));
于 2012-04-17T22:13:40.697 回答