以可读且简短的格式显示日期/时间
我以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你还有年、月、日。你不需要别的什么。只需检查年是否为 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"