以可读和简短的格式显示日期/时间

时间:2011-07-22 14:12:25

标签: php unix-timestamp

我将日期和时间存储为unix时间戳格式(d1 = 1387721302,d2 = 1311343703),并希望查看日期差异(过去,现在和将来)。

2周前

15天前

2分钟前

3个月前

1年前

从现在起9个月

从现在起4天

..希望你抓住漂移? 而不是“0年,4个月,4天” 会欣赏某种功能或某种功能。 这实际上就是我现在使用的

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);

谢谢

这是我尝试做的但不准确但作为指导。 任何人都会帮忙。

if ($years >= 1)
{
    $dis_date = printf("%d years\n", $years);

}elseif ($years <= 0 && $months >= 0)
{
    $dis_date = printf("%d months, %d days\n", $months, $days);

}elseif ($years <=0 && $months <= 0 && $days >= 0)
{
    $dis_date = printf("%d days\n", $days);
}else{
    $dis_date = printf("%d years, %d months, %d days\n", $years, $months, $days);
}


echo $dis_date;

2 个答案:

答案 0 :(得分:2)

function f1 ($time)
{
  $rel = time() - $time;
  if ($rel < 60 * 60)
    $rel .= ' min ago';
  elseif ($rel < 60 * 60 * 24)
    $rel .= ' hours ago';
  elseif ($rel < 60 * 60 * 24 * 30)
    $rel .= ' days ago';
....
  return $rel 

}

答案 1 :(得分:1)

你有几年,几个月和几天。你什么都不需要。只需检查年份是否为0,然后月份为0,然后是天数为0.然后相应地打印数据。

实际上有这种控制是好的,你可能想写“60年”而不是“60年2个月3天”