CodeIgniter time ago – How to get it done!

in PHP

In today’s web, its much better to use x hour ago, day ago , month ago , a year ago etc. CodeIgniter has a built-in function in date_helper that allows you to convert any date/time to “xx Time ago” style, its called timespan() it takes 2 arguments the first one is the date/time you want to convert to “time ago style” and the second one should be current date time, below is an example show you how to use CodeIgniter timespan function

First you will need to load the date helper

$this->load->helper(‘date’);

Now lets use the function in any of our controllers

timespan(1326927423, time() ) . “ago” ;

That will outputs something like

2 hours ago

Note that 1326927423 is a UNIX date format, see this page to learn about date formats and how to convert them.

0 Comments

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>