13 Oct, 2008 in
Programming by
Roger
recently used this feature in a couple different applications on various platforms.
Take this:
2008-10-13 17:47:33
and turn it into this:
5 minutes 16 seconds ago
Please share links to code snippets for any other languages in the comments!
Here’s the C# code:
1: /// <summary>
2: /// Compares to DateTimes and converts the result to an easy human readable format.
3: /// </summary>
4: /// <param name="time">A past or future DateTime.</param>
5: /// <param name="relativeTo">Relative to this time.</param>
6: /// <returns></returns>
7: public string ToRelativeTime(DateTime time, DateTime relativeTo)
8: {
9: TimeSpan ts = relativeTo.Subtract(time).Duration();
10: string DateFormat = "d MMMM";
11: string dir = (relativeTo > time) ? "Ago" : "To go";
12:
13: if (relativeTo.Year != time.Year)
14: DateFormat += " yyyy";
15:
16: if (ts.Days < 360)
17: {
18: //Months
19: if (ts.Days >= 30)
20: return string.Format("{0} ({1} Months {2})", time.ToString(DateFormat), (int)(ts.Days / 30), dir);
21:
22: //Days
23: if (ts.Days > 0)
24: return string.Format("{0} ({1} Days {2})", time.ToString(DateFormat), ts.Days, dir);
25:
26: //hours
27: if (ts.Hours > 0)
28: return string.Format("{0} Hours {1} Minutes {2}", ts.Hours, ts.Minutes, dir);
29:
30: //minutes
31: if (ts.Minutes > 0)
32: return string.Format("{0} Minutes {1} Seconds {2}", ts.Minutes, ts.Seconds, dir);
33:
34: //seconds
35: return string.Format("{0} Seconds {1}", ts.Seconds, dir);
36: }
37:
38: return time.ToString(DateFormat);
39: }
And the PHP code: (originally provided by sacklun.ch)
1: // Written by Jeremy Wagner (http://blog.sacklun.ch)
2: // A static class that takes a Unix timestamp as input
3: // and calculates the relative time that has since passed.
4: //
5: // I don't care what the hell you do with this.
6: // Modify it, redistribute it, tell your grandmother
7: // about it, or laugh at it.
8: function createTimeString($timeStamp, $format="long", $dateFormat="n/j/Y")
9: {
10: // Take the current time and create the difference
11: $timeDifference = time() - $timeStamp;
12:
13: // Check the length of time passed since seconds
14: switch($timeDifference)
15: {
16: // Date is less than an hour old
17: case $timeDifference <= 3600:
18: $minutes = floor($timeDifference / 60);
19: $seconds = $timeDifference - ($minutes * 60);
20:
21: // Choose correct pluralizations
22: if($minutes == 1)
23: {
24: $minuteString = "minute";
25: }
26: else
27: {
28: $minuteString = "minutes";
29: }
30:
31: if($seconds == 1)
32: {
33: $secondString = "second";
34: }
35: else
36: {
37: $secondString = "seconds";
38: }
39:
40: switch($format)
41: {
42: case "short":
43: return $minutes . "m " . $seconds . "s ago";
44: break;
45:
46: default:
47: return $minutes . " " . $minuteString . " " . $seconds . " " . $secondString . " ago";
48: break;
49: }
50: break;
51:
52: // Date is less than a day old
53: case $timeDifference <= 86400:
54: $hours = floor($timeDifference / 60 / 60);
55: $minutes = floor(($timeDifference - ($hours * 60 * 60)) / 60);
56:
57: // Choose correct pluralizations
58: if($hours == 1)
59: {
60: $hourString = "hour";
61: }
62: else
63: {
64: $hourString = "hours";
65: }
66:
67: if($minutes == 1)
68: {
69: $minuteString = "minute";
70: }
71: else
72: {
73: $minuteString = "minutes";
74: }
75:
76: switch($format)
77: {
78: case "short":
79: return $hours . "h " . $minutes . "m ago";
80: break;
81:
82: default:
83: return $hours . " " . $hourString . " " . $minutes . " " . $minuteString . " ago";
84: break;
85: }
86: break;
87:
88: // Date is less than a week old.
89: case $timeDifference <= 604800:
90: $days = floor($timeDifference / 86400);
91: $hours = floor(($timeDifference - ($days * 86400)) / 60 / 60);
92:
93: // Choose correct pluralizations
94: if($days == 1)
95: {
96: $dayString = "day";
97: }
98: else
99: {
100: $dayString = "days";
101: }
102:
103: if($hours == 1)
104: {
105: $hourString = "hour";
106: }
107: else
108: {
109: $hourString = "hours";
110: }
111:
112: switch($format)
113: {
114: case "short":
115: return $days . "d " . $hours . "h ago";
116: break;
117:
118: default:
119: return $days . " " . $dayString . " " . $hours . " " . $hourString . " ago";
120: break;
121: }
122: break;
123:
124: // Older than one week. Just return the absolute date.
125: default:
126: return date($dateFormat, $timeStamp);
127: break;
128: }
129: }
12/2/09 – I inserted the actual snippets since both source links have gone down.
Anoop M S | July 21st, 2011 at 2:10 pm #
A much simplified version here
http://www.waytocoding.com/2011/07/how-to-convert-normal-date-time-to.html