Part of the EllisLab Network
   
1 of 2
1
Nicer dates (extending date_helper.php)
Posted: 22 February 2009 09:22 AM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  404
Joined  03-28-2008

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:

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

The my_date_helper.php file:

<?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/)

$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

 Signature 

———————————————————————————————————————————-
Imac 24” C2D 3.06Ghz / 4GB RAM / Geforce 8800GS 512MB
Macbook Pro 15” C2D 2.53Ghz / 4GB RAM / NVIDIA GeForce 9400M + 9600M GT 512MB
iPhone 3G 16Gb Black

http://www.rockkarusellen.se

Profile
 
 
Posted: 23 February 2009 10:59 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  173
Joined  03-27-2008

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

Profile
 
 
Posted: 23 February 2009 11:19 AM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2517
Joined  06-11-2007
AgentPhoenix - 23 February 2009 10:59 AM

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

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()?

 Signature 

Blog | Twitter | GitHub
————————-
CodeIgniter 2: Everything you need to know
————————
PyroCMS - open source modular CMS built with CodeIgniter
CleverAndy - get money for un-used concept designs
————————
Libraries: Asset, Dwoo, Cache, cURL, CLI, REST, Template

Profile
 
 
Posted: 23 February 2009 11:20 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006
Phil Sturgeon - 23 February 2009 11:19 AM

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

now is not a php function AFAIK

Profile
 
 
Posted: 23 February 2009 11:43 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  404
Joined  03-28-2008
xwero - 23 February 2009 11:20 AM
Phil Sturgeon - 23 February 2009 11:19 AM

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

now is not a php function AFAIK

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

 Signature 

———————————————————————————————————————————-
Imac 24” C2D 3.06Ghz / 4GB RAM / Geforce 8800GS 512MB
Macbook Pro 15” C2D 2.53Ghz / 4GB RAM / NVIDIA GeForce 9400M + 9600M GT 512MB
iPhone 3G 16Gb Black

http://www.rockkarusellen.se

Profile
 
 
Posted: 23 February 2009 12:04 PM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

Have you seen the now function code? It is a bit messy. This is the cleaned up version

function now()
{
   
return (strtolower(config_item('time_reference')) == 'gmt') ? gmmktime() : time() ;
}
Profile
 
 
Posted: 23 February 2009 02:16 PM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  404
Joined  03-28-2008
xwero - 23 February 2009 12:04 PM

Have you seen the now function code? It is a bit messy. This is the cleaned up version

function now()
{
   
return (strtolower(config_item('time_reference')) == 'gmt') ? gmmktime() : time() ;
}

Nope, did not check it out. And still havent!
Your code look great though… smile

 Signature 

———————————————————————————————————————————-
Imac 24” C2D 3.06Ghz / 4GB RAM / Geforce 8800GS 512MB
Macbook Pro 15” C2D 2.53Ghz / 4GB RAM / NVIDIA GeForce 9400M + 9600M GT 512MB
iPhone 3G 16Gb Black

http://www.rockkarusellen.se

Profile
 
 
Posted: 23 February 2009 02:34 PM   [ Ignore ]   [ # 7 ]  
Grad Student
Rank
Total Posts:  76
Joined  07-15-2007
xwero - 23 February 2009 12:04 PM

Have you seen the now function code? It is a bit messy. This is the cleaned up version

function now()
{
   
return (strtolower(config_item('time_reference')) == 'gmt') ? gmmktime() : time() ;
}

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!)

Profile
 
 
Posted: 23 February 2009 02:37 PM   [ Ignore ]   [ # 8 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1095
Joined  05-17-2008

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

Profile
 
 
Posted: 23 February 2009 02:59 PM   [ Ignore ]   [ # 9 ]  
Grad Student
Rank
Total Posts:  76
Joined  07-15-2007

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!!

Profile
 
 
Posted: 23 February 2009 04:01 PM   [ Ignore ]   [ # 10 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  104
Joined  01-21-2009

Searching for weird characters in Google is awful, especially in the context of programming.  It’s pretty ruthless about ignoring characters. Every once in awhile Google Code Search can help… http://www.google.com/codesearch

 Signature 

Do you use CSS or JavaScript? Carabiner makes your life easier.  I promise.

CI-Disqus makes playing with the Disqus API a snap.

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120493 Total Logged-in Users: 41
Total Topics: 126564 Total Anonymous Users: 7
Total Replies: 665426 Total Guests: 370
Total Posts: 791990    
Members ( View Memberlist )