Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
(Not help) function human_time()
Old 09-25-2011, 07:02 PM (Not help) function human_time()
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Hey guys, I got bored so I made a function that replaces your Epoch (UNIX) timestamp with an actual string that makes sense for humans.

PHP Code:
<?php

/*****************
 * human_time (c) Scott Kaye
 * usage:  pass human_time an Epoch timestamp, and it will return a nicer value, such as '4 minutes ago' or '8 seconds ago'
 *****************/

$sample=1316991051//Sample Epoch time to test it out!
 
function human_time($t){
    
$cT=time();
    if(
date('Y',$cT)==date('Y',$t)){
        
$o='Some time this year';
        if(
date('n',$cT)==date('n',$t)){
            
$o='This month';
            if(
date('W',$cT)==date('W',$t)){
                
$o='This week';
                if(
date('j',$cT)==date('j',$t)){
                    
$o='Today';
                    if(
date('g',$cT)==date('g',$t)){
                        
$o='Less than an hour ago';
                        if(
date('i',$cT)==date('i',$t)){
                            
$a=date('s',$cT)-date('s',$t);
                            
$p=($a>1)?"s":null;
                            
$o=$a." second{$p} ago";
                        }
                        else {
                            
$a=date('i',$cT)-date('i',$t);
                            
$p=($a>1)?"s":null;
                            
$o=$a." minute{$p} ago";
                        }
                    }
                    else {
                        
$a=date('g',$cT)-date('g',$t);
                        
$p=($a>1)?"s":null;
                        
$o=$a." hour{$p} ago";
                    }
                }
                else {
                    
$a=date('j',$cT)-date('j',$t);
                    
$p=($a>1)?"s":null;
                    
$o=$a." day{$p} ago";
                }
            }
            else {
                
$a=date('W',$cT)-date('W',$t);
                
$p=($a>1)?"s":null;
                
$o=$a." week{$p} ago";
            }
        }
        else {
            
$a=date('n',$cT)-date('n',$t);
            
$p=($a>1)?"s":null;
            
$o=$a." month{$p} ago";
        }
    }
    else {
        
$a=date('Y',$cT)-date('Y',$t);
        
$p=($a>1)?"s":null;
        
$o=$a." year{$p} ago";
    }
    return 
$o;
}

echo 
human_time($sample);

?>
It's probably full of bugs, but meh. Took me 5 minutes (copy-paste, copy-paste, copy-paste...).

As always, though, if you do find anything wrong with it, please let me know and I'll attempt to fix it!

EDIT: OMG, my 666th post. Oh, dis is bad! (TF2 reference)
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-26-2011, 01:24 PM Re: (Not help) function human_time()
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0


I once did a programmer's version of this - "it's on time", "yes, definitely tomorrow", "I don't know", "if you'd just **** off and let me get on with it" sorts of strings.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to (Not help) function human_time()
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.10140 seconds with 12 queries