TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   TimeElapsed (http://www.talkphp.com/absolute-beginners/4445-timeelapsed.html)

 Sirupsen 05-27-2009 07:59 PM

TimeElapsed

Hello! I'm trying to make a scrupt which calculates estimated time since post, like on twitter. I kinda just want it to show minutes, untill its hours, show hours untill its days ago - and when it's at days, show the full date. But would I do this?

But yeah, I'm really really unsure how to do this? Anyone can help me a bit? Here's the code I have so far, if someone know a site or script for this I'd love a look on it..:

PHP Code:

```     function calcElapsedTime(\$time){        \$diff = time()-\$time;        \$yearsDiff = floor(\$diff/60/60/24/365);        \$diff -= \$yearsDiff*60*60*24*365;        \$monthsDiff = floor(\$diff/60/60/24/30);        \$diff -= \$monthsDiff*60*60*24*30;        \$weeksDiff = floor(\$diff/60/60/24/7);        \$diff -= \$weeksDiff*60*60*24*7;        \$daysDiff = floor(\$diff/60/60/24);        \$diff -= \$daysDiff*60*60*24;        \$hrsDiff = floor(\$diff/60/60);        \$diff -= \$hrsDiff*60*60;        \$minsDiff = floor(\$diff/60);        \$diff -= \$minsDiff*60;        \$secsDiff = \$diff;                \$ago = 'ago';        \$years = \$yearsDiff.' year'.((\$yearsDiff <> 1) ? "s" : "");        \$months = \$monthsDiff.' month'.((\$monthsDiff <> 1) ? "s" : "");        \$weeks = \$weeksDiff.' week'.((\$weeksDiff <> 1) ? "s" : "");        \$days = \$daysDiff.' day'.((\$daysDiff <> 1) ? "s" : "");        \$hours = \$hrsDiff.' hour'.((\$hrsDiff <> 1) ? "s" : "");        \$mins = \$minsDiff.' minute'.((\$minsDiff <> 1) ? "s" : "");        \$secs = \$secsDiff.' second'.((\$secsDiff <> 1) ? "s" : "");                if(\$years < 1) {            \$years = "";        }                    if(\$months < 1) {            \$months = "";        }                        if(\$weeks < 1) {            \$weeks = "";        }                if(\$days < 1) {            \$days = "";        }                        if(\$hours < 1) {            \$hours = "";        }                if(\$mins < 1) {            \$mins = "";        }                if(\$secs < 1) {            \$secs = "";        }                return ('                    '.\$years.'             '.\$months.'            '.\$weeks.'            '.\$days.'             '.\$hours.'             '.\$mins.'             '.\$secs.'            '.\$ago.'                ');}  ```

 codefreek 05-28-2009 01:27 AM

PHP/TIME

I think this should help.

-Cf

 Sirupsen 05-28-2009 05:52 AM

Thanks a lot! I found what I needed, I had only been looking at Date function on php net. :)

 All times are GMT. The time now is 08:33 AM.