PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Aug 23, 2019 11:43 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Mon Mar 08, 2010 4:42 am 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 5001
Location: Indiana
Cool that is very valuable information. So I suppose when using lookup tables vs a bunch of if's (if worried about performance), it would depend on the number and complexity of the evaluations.

Of course, when profiling code, if this is the biggest speed problem then one is in great shape!

_________________
- A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.


Top
 Profile  
 
PostPosted: Mon Mar 08, 2010 10:58 am 
Offline
The Ninja Space Mod
User avatar

Joined: Fri Aug 05, 2005 1:53 pm
Posts: 6424
Location: Paradise, CA
I should mention I took a lot of inspiration from Django's "humanize" for my code. So you could look at that as well. :)


Top
 Profile  
 
PostPosted: Tue Aug 28, 2012 9:33 pm 
Offline
Forum Newbie

Joined: Tue Aug 28, 2012 5:34 pm
Posts: 1
Hi everyone,

Here is my example of time_passed and it seems to perform quicker and also caters for different formats passed in and for time in the future

Syntax: [ Download ] [ Hide ]
       /**
        *
        * This function calculates how long ago a certain
        * date passed in was.
        *
        */

       
        define('SEC_IN_MINUTE',60); /* How many seconds are in a minute */
        define('SEC_IN_HOUR',3600); /* How many seconds are in an hour */
        define('SEC_IN_DAY',86400);     /* How many seconds are in a day */
        define('SEC_IN_WEEK',604800); /* How many seconds are in a week */
        define('SEC_IN_MONTH',2630880); /* How many seconds are in a month */
        define('SEC_IN_YEAR',31556926); /* How many seconds are in a year */
       
        function md_time_passed($d) {
               
                /**
                 * This code block checks to see what type of date is
                 * passed in and then obtains the difference in
                 * seconds.
                 */


                if (empty($d)) {
                   return('No date provided');
                }

                if ($d instanceof DateTime) {
                        $c = new DateTime();
                        $cTime = $c->getTimestamp();
                        $dTime = $d->getTimestamp();           
                        $diff = $cTime-$dTime; 
                } elseif (is_array($d) && isset($d["mon"])) {
                        $d = $d[0];
                        $c = time();
                        $diff = $c-$d; 
                } elseif (is_string($d)) {
                        $d = strtotime($d);
                        $c = time();
                        $diff = $c-$d; 
                } else {
                        $c = time();
                        $diff = $c-$d; 
                }
               
                $agostate = ($diff>=0) ? "ago" : "from now";
               
                $diff = abs($diff);
               
                /**
                 * This section of code converts the seconds in
                 * single units based on whether it is a year, month,
                 * week, day, hour, minute or second
                 */

       
                if ($diff>=SEC_IN_YEAR) {
                        $diff = floor($diff / SEC_IN_YEAR);                                            
                        $suffix = "year";
                } elseif ($diff>=SEC_IN_MONTH) {
                        $diff = floor($diff / SEC_IN_MONTH);                                           
                        $suffix = "month";             
                } elseif ($diff>=SEC_IN_WEEK) {
                        $diff = floor($diff / SEC_IN_WEEK);                                            
                        $suffix = "week";                      
                } elseif ($diff>=SEC_IN_DAY) {
                        $diff = floor($diff / SEC_IN_DAY);                                             
                        $suffix = "day";                               
                } elseif ($diff>=SEC_IN_HOUR) {
                        $diff = floor($diff / SEC_IN_HOUR);                                            
                        $suffix = "hour";                      
                } elseif ($diff>=SEC_IN_MINUTE) {
                        $diff = floor($diff / SEC_IN_MINUTE);                                          
                        $suffix = "minute";                    
                } elseif ($diff>0) {
                        $suffix = "second";                                    
                } else {
                        return("Just Now");
                }
               
                return(($diff==1) ? "{$diff} {$suffix} {$agostate}" : "{$diff} {$suffix}s {$agostate}");
        }
 


Please let me know your thoughts

Marc


Top
 Profile  
 
PostPosted: Tue Aug 28, 2012 10:38 pm 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6887
Looks great :)

_________________
Image


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group