1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

relative time in cakephp

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


var $view = "App";

your app.php looks something like this

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<60)
 return $diff . " second" . $this->plural($diff) . " ago";
 $diff = round($diff/60);
 if ($diff<60)
 return $diff . " minute" . $this->plural($diff) . " ago";
 $diff = round($diff/60);
 if ($diff<24)
 return $diff . " hour" . $this->plural($diff) . " ago";
 $diff = round($diff/24);
 if ($diff<7)
 return $diff . " day" . $this->plural($diff) . " ago";
 $diff = round($diff/7);
 if ($diff<4)
 return $diff . " week" . $this->plural($diff) . " ago";
 return "on " . date("F j, Y", strtotime($date));
 }
}


source

Share:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • MySpace
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Twitter
  • Yahoo! Bookmarks
  • #1
    Posted by Robert on August 6th, 2009 at 4:30 PM

    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
    Posted by Funky Dude on August 6th, 2009 at 4:36 PM

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

  • #3
    Posted by Robert on August 6th, 2009 at 5:03 PM

    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
    Posted by Funky Dude on August 6th, 2009 at 5:11 PM

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

  • #5
    Posted by Miles Johnson on August 6th, 2009 at 6:52 PM

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

Share your opinion! Post your thoughts.