Skip navigation

Tag Archives: date

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function plural($num) {
 if ($num != 1)
 return "s";
}

function getRelativeTime($date) {
 $diff = time() - strtotime($date);
 if ($diff<60)
 return $diff . " second" . plural($diff) . " ago";
 $diff = round($diff/60);
 if ($diff<60)
 return $diff . " minute" . plural($diff) . " ago";
 $diff = round($diff/60);
 if ($diff<24)
 return $diff . " hour" . plural($diff) . " ago";
 $diff = round($diff/24);
 if ($diff<7)
 return $diff . " day" . plural($diff) . " ago";
 $diff = round($diff/7);
 if ($diff<4)
 return $diff . " week" . plural($diff) . " ago";
 return "on " . date("F j, Y", strtotime($date));
}<span>

If you want to use this in your view files, You need a create a file call app.php in view folder.
In your app_controller.php declare a variable

1
var $view = "App";

your app.php looks something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class AppView extends View{

 function convertMysqlDate($dateString)
 {
 return date('M d Y h:s A',strtotime($dateString));
 }
 function plural($num) {
 if ($num != 1)
 return "s";
 }

 function getRelativeTime($date) {
 $diff = time() - strtotime($date);
 if ($diff&lt;60)
 return $diff . " second" . $this-&gt;plural($diff) . " ago";
 $diff = round($diff/60);
 if ($diff&lt;60)
 return $diff . " minute" . $this-&gt;plural($diff) . " ago";
 $diff = round($diff/60);
 if ($diff&lt;24)
 return $diff . " hour" . $this-&gt;plural($diff) . " ago";
 $diff = round($diff/24);
 if ($diff&lt;7)
 return $diff . " day" . $this-&gt;plural($diff) . " ago";
 $diff = round($diff/7);
 if ($diff&lt;4)
 return $diff . " week" . $this-&gt;plural($diff) . " ago";
 return "on " . date("F j, Y", strtotime($date));
 }
}


source