PHP:是否有一个以人类可读格式转换日期字符串的函数?

时间:2009-11-19 13:11:53

标签: php datetime

我想将“19.11.2009 14:00”之类的日期字符串转换为现在的年龄,例如“2分钟”或“1周”或“2天”

是否有一些代码?

4 个答案:

答案 0 :(得分:8)

$dateString = strtotime('19.11.2009 14.00');
$now = time();

$time = $dateString - $now;

if($time > 60 && $time < 3600) echo $time/60.' minutes remaining';
else if($time > 3600 && $time < 86400) echo $time/3600.' hours remaining';
else if($time > 86400 && $time < 604800) echo $time/86400.' days remaining';
else if($time > 604800 && $time < 18144000) echo $time/604800.' weeks remaining';
else if($time > 18144000 && $time < 217728000) echo $time/18144000.' months remaining';
else if($time > 217728000) echo $time/217728000.' years remaining';

答案 1 :(得分:5)

像这样的东西

define('MINUTE',60);
define('HOUR',60*MINUTE);
define('DAY',24*HOUR);
define('WEEK',7*DAY);
define('MONTH',30*DAY);

$pastDate=strtotime($dateString);
$seconds=time()-$pastDate;
if ($seconds>MONTH)
  return $seconds/MONTH . " months";
if ($seconds>WEEK)
  return $seconds/WEEK . " weeks";
if ($seconds>DAY)
  return $seconds/DAY . " days";
if ($seconds>HOUR)
  return $seconds/HOUR . " hours";
if ($seconds>MINUTE)
  return $seconds/MINUTE . " minutes";
return $seconds . " seconds";

答案 2 :(得分:5)

如果您使用的是PHP 5.3,则还可以使用DateTime:diff

$start = new DateTime('now');
$time_span = $start->diff(new DateTime($dateString));
var_dump($time_span);

答案 3 :(得分:2)

这可能不是你想要的100% - 其他人给你很好的答案 - 但这样的事情可能是“人类可读”日期格式的一个很好的替代品。

我从很久以前使用的一些代码中挖出来了。我有一段时间没有测试过这个,但最后我还记得它很棒。我想复制像Facebook使用的东西,比如“5秒前”,但它也适用于未来,使用“in ...”代替“......之前”。你可以根据需要修改它以获得尽可能多的细节。

/**
 * Returns the amount of time that has passed from the current date
 * or the amount of time from the current date until the specified date
 *
 * Returns in the form of a partial sentence. Some examples:
 *
 * In 25 days
 * Tomorrow
 * Yesterday
 * 4 months ago
 * Next month
 * Last month
 * (etc)
 *
 * @param string $date
 * @return string
 */
public static function calculateHowLong($date) {
  // start by converting to unix time
  $when = date("U", strtotime($date));
  $isPast = ($when < time());

  $how_long = abs(time() - $when);
  if ($how_long < 60) {
    $return =  "{$how_long} seconds";
    if ($isPast) $return .= " ago"; else $return = "In {$return}";

  } elseif ($how_long < 60 * 60) {
    $return =  (int) ($how_long / 60) . " minutes";
    if ($isPast) $return .= " ago"; else $return = "In {$return}";

  } elseif ($how_long < 60 * 60 * 24) {
    $return =  (int) ($how_long / (60 * 60)) . " hours";
    if ($isPast) $return .= " ago"; else $return = "In {$return}";

  } elseif ($how_long < 60 * 60 * 24 * 2) {
    if ($isPast) $return = "Yesterday"; else $return = "Tomorrow";

  } elseif ($how_long < 60 * 60 * 24 * 7) {
    $return =  (int) ($how_long / (60 * 60 * 24)) . " days";
    if ($isPast) $return .= " ago"; else $return = "In {$return}";

  } elseif ($how_long < 60 * 60 * 24 * 13) {
    if ($isPast) $return = "Last week"; else $return = "Next week";

  } elseif ($how_long < 60 * 60 * 24 * 7 * 4) {
    $return =  (int) ($how_long / (60 * 60 * 24 * 7)) . " weeks";
    if ($isPast) $return .= " ago"; else $return = "In {$return}";

  } elseif ($how_long < 60 * 60 * 24 * 30 * 2) {
    if ($isPast) $return = "Last month"; else $return = "Next month";

  } elseif ($how_long < 60 * 60 * 24 * 30 * 12) {
    $return =  (int) ($how_long / (60 * 60 * 24 * 30)) . " months";
    if ($isPast) $return .= " ago"; else $return = "In {$return}";

  } else {
    if ($isPast) $return = "More than 1 year ago"; else $return = "In more than 1 year";
  }

  return $return;
}

这可能有点草率,但随意让它变得更好。