문서의 이전 판입니다!


Php 친절한 날짜 표시

// 츌쳐: http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time
const int SECOND = 1;
const int MINUTE = 60 * SECOND;
const int HOUR = 60 * MINUTE;
const int DAY = 24 * HOUR;
const int MONTH = 30 * DAY;
 
var ts = new TimeSpan(DateTime.UtcNow.Ticks - yourDate.Ticks);
double delta = Math.Abs(ts.TotalSeconds);
 
if (delta < 1 * MINUTE)
{
  return  "방금";
}
if (delta < 5 * MINUTE)
{
  return "몇분전";
}
if (delta < 45 * MINUTE)
{
  return ts.Minutes + " 분전";
}
if (delta < 90 * MINUTE)
{
  return "한시간 전";
}
if (delta < 24 * HOUR)
{
  return ts.Hours + " 시간 전";
}
if (delta < 48 * HOUR)
{
  return "어제";
}
if (delta < 30 * DAY)
{
  return ts.Days + " 일 전";
}
if (delta < 12 * MONTH)
{
  int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
  return months <= 1 ? "한달전" : months + " 개월전";
}
else
{
  int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
  return "오래전";
}