Skip navigation

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

5 Comments

  1. Or you could pass this ineffective and obscure implementation and just use TimeHelper::timeAgoInWords()

    It’s great that you’re being productive with CakePHP, but I don’t understand why you’re in the Planet CakePHP aggregator.

  2. thanks. didnt know that helper. Wait, are you saying i am not good enough or something?

  3. I’m not judging worth or merit, and I wasn’t trying to be insulting.

    Speaking from a Cakish stance however, this code should be neatened, refactored and put into a Helper. If it does what you want, then job well done, but cutting and pasting some functions into a class named AppView doesn’t make this a CakePHP post.

    That these methods *should *be in a Helper makes this a “learning about Cake” blog, and not something I would expect to be aggregated into an almost-official feed.

    http://book.cakephp.org/ is an invaluable resource, and one I wish had been around when I started using Cake. It also documents TimeHelper and other useful Helpers like TextHelper and Inflector.

    Again: I’m not flaming or trolling, just pointing out what I see.

  4. i guess you are right. this is not the proper way of doing things in cake.

  5. Like robert said, you could use $time->timeAgoInWords() or $time->relativeTime(), using the Time helper. But nice code none the less.


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>