Welcome Guest, Not a member yet? Register   Sign In
Nicer dates (extending date_helper.php)
#1

[eluser]Johan André[/eluser]
Hey!

I created an extension to the date_helper to output nicer dates.
The function assumes a timestamp OR a date in the format 2009-02-22 12:00:00.

It will compare the input to the current time and create something like:

"4 minutes ago" or "1 month ago"

The helper uses the language class for different languages so you'll have to add some keys to it to support the new function (included at bottom of post).

Usage in views:

Code:
<p>The post was created &lt;?=relative_time('2009-01-22 12:00:00');?&gt;</p>

The my_date_helper.php file:

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if( ! function_exists('relative_time'))
{
    function relative_time($datetime)
    {
        $CI =& get_instance();
        $CI->lang->load('date');
        
        if(!is_numeric($datetime))
        {
            $val = explode(" ",$datetime);
           $date = explode("-",$val[0]);
           $time = explode(":",$val[1]);
           $datetime = mktime($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]);
        }
        
        $difference = time() - $datetime;
        $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
        $lengths = array("60","60","24","7","4.35","12","10");

        if ($difference > 0)
        {
            $ending = $CI->lang->line('date_ago');
        }
        else
        {
            $difference = -$difference;
            $ending = $CI->lang->line('date_to_go');
        }
        for($j = 0; $difference >= $lengths[$j]; $j++)
        {
            $difference /= $lengths[$j];
        }
        $difference = round($difference);
        
        if($difference != 1)
        {
            $period = strtolower($CI->lang->line('date_'.$periods[$j].'s'));
        } else {
            $period = strtolower($CI->lang->line('date_'.$periods[$j]));
        }
        
        return "$difference $period $ending";
    }
        
    
}

The additions to date_lang.php (in your prefered language ofcourse, place in app/languages/your_language/)

Code:
$lang['date_decade'] = "Decade";
$lang['date_decades'] = "Decades";
$lang['date_ago']    = "ago";
$lang['date_to_go']    = "to go";

To me this was quite useful, hope someone else likes it.
I will continue to work on it to output "about 5 minutes ago" and stuff like that.

Cheers!

EDIT: Just remembered that I should include a link to the original function, although rewritten quite alot. http://snipplr.com/view.php?codeview&id=12177
#2

[eluser]AgentPhoenix[/eluser]
This looks like it could be really useful! I'd love to see this be able to adjust for timezone and daylight savings time as well. Should be pretty easy to add both as parameters to the function and then take them into account when creating the date at the start. Smile
#3

[eluser]Phil Sturgeon[/eluser]
[quote author="AgentPhoenix" date="1235426350"]This looks like it could be really useful! I'd love to see this be able to adjust for timezone and daylight savings time as well. Should be pretty easy to add both as parameters to the function and then take them into account when creating the date at the start. Smile[/quote]

Good stuff. I wrote a similar function a while ago but was not anywhere near as clean as this one.

Should this not be using now() instead of time()?
#4

[eluser]xwero[/eluser]
[quote author="Phil Sturgeon" date="1235427553"]Should this not be using now() instead of time()?[/quote]
now is not a php function AFAIK
#5

[eluser]Johan André[/eluser]
[quote author="xwero" date="1235427636"][quote author="Phil Sturgeon" date="1235427553"]Should this not be using now() instead of time()?[/quote]
now is not a php function AFAIK[/quote]

Actually now() is a function in the date-helper (which my function extends) so probably it would be the best.. Smile
#6

[eluser]xwero[/eluser]
Have you seen the now function code? It is a bit messy. This is the cleaned up version
Code:
function now()
{
   return (strtolower(config_item('time_reference')) == 'gmt') ? gmmktime() : time() ;
}
#7

[eluser]Johan André[/eluser]
[quote author="xwero" date="1235430297"]Have you seen the now function code? It is a bit messy. This is the cleaned up version
Code:
function now()
{
   return (strtolower(config_item('time_reference')) == 'gmt') ? gmmktime() : time() ;
}
[/quote]

Nope, did not check it out. And still havent!
Your code look great though... Smile
#8

[eluser]jtotheb[/eluser]
[quote author="xwero" date="1235430297"]Have you seen the now function code? It is a bit messy. This is the cleaned up version
Code:
function now()
{
   return (strtolower(config_item('time_reference')) == 'gmt') ? gmmktime() : time() ;
}
[/quote]

What does the "?" and then the subsequent ":" do in that code? I've seen that syntax style used before but never known what it means / does. Would someone mind explaining to an idiot like me what that means / is called and where i can read more about it?

Cheers! (sorry to thread hijack!)
#9

[eluser]pistolPete[/eluser]
It's called ternary operator and it is a shorthand for an if/else statement.
Have a look at the php documentation: http://php.net/language.operators.comparison
#10

[eluser]jtotheb[/eluser]
Excellent, cheers! I was struggling to work out how to do a google search for that syntax to try and find out what it is!!




Theme © iAndrew 2016 - Forum software by © MyBB