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

发布于 2024-11-25 12:31:16 字数 1125 浏览 1 评论 0原文

我以 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;

I stored my dates and times in unix timestamp format (d1=1387721302, d2=1311343703) and would like to view date differences(past, present and future) in say.

2 Weeks ago

15 Days ago

2 Minutes ago

3 Months ago

1 Year ago

9 Months From Now

4 Days From Now

etc.

..hope you catch the drift?
instead of "0 Years, 4 Months, 4 Days"
Would appreciate a function or some sort.
This is actually what I use now

$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);

Thank you

This is what I tried doing but not accurate but as a guide.
Anybody pls help.

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身以后 2024-12-02 12:31:16
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 

}
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 

}
百善笑为先 2024-12-02 12:31:16

你还有年、月、日。你不需要别的什么。只需检查年是否为 0,然后月为 0,然后天为 0。然后相应地打印数据。

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

You have years, months and days. You need nothing else. Just check if years is 0, then month is 0, then days is 0. Then print the data accordingly.

Having that kind of control is good actually, you may want to write "60 years" instead of "60 years 2 months 3 days"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文