PHP Time Function (Seconds,Minutes,Hours,Days,Weeks) since.

Reply to topic Page 1 of 1 [ 5 posts ]
Author Message

Offline Messiah-Wayne

Gold Member


User avatar


Joined: Mon Jul 28, 2008 3:06 pm

Posts: 179

Location: Ireland

PostPosted: Sat Sep 20, 2008 3:19 pm   Post subject: PHP Time Function (Seconds,Minutes,Hours,Days,Weeks) since.   

[php]
function time_since($time){

$now = mktime();
$seconds_since = $now - $time;

if($seconds_since < 60){
return $seconds_since.' seconds ago';
}
else if($seconds_since > 59 && $seconds_since < 120){
return '1 minute ago';
}
else if($seconds_since > 119 && $seconds_since < 3600){
$minutes = round($seconds_since / 60);
return $minutes.' minutes ago';
}
else if($seconds_since > 3599 && $seconds_since < 86400){
$hours = round($seconds_since / 3600);
if($hours == 1){
return "1 hour ago";
}
else{
return $hours.' hours ago';
}
}
else if($seconds_since > 86400 && $seconds_since < 604800){
$days = round($seconds_since/86400);
if($days == 1){
return "1 day ago";
}
else{
return $days.' days ago';
}
}
else if($seconds_since > 604799){
$weeks = round($seconds_since/604800);
if($weeks == 1){
return "1 week ago";
}
else{
return $weeks.' weeks ago';
}
}
}
[/php]

_________________
http://WayneWhitty.com
http://GTFONOOB.com

BBcode was invented because programmers were too lazy or afraid to deal with real HTML. So they invented their own markup.
Top Profile E-mail 

Offline Messiah-Wayne

Gold Member


User avatar


Joined: Mon Jul 28, 2008 3:06 pm

Posts: 179

Location: Ireland

PostPosted: Sat Sep 20, 2008 3:20 pm   Post subject: Re: PHP Time Function (Seconds,Minutes,Hours,Days,Weeks) since.   

Sorry, I'm too used to PHP Freaks' PHP BBcode

function time_since($time){
         
         $now = mktime();
         $seconds_since = $now - $time;
         
         if($seconds_since < 60){
            return $seconds_since.' seconds ago';
         }
         else if($seconds_since > 59 && $seconds_since < 120){
            return '1 minute ago';
         }
         else if($seconds_since > 119 && $seconds_since < 3600){
            $minutes = round($seconds_since / 60);
            return $minutes.' minutes ago';
         }
         else if($seconds_since > 3599 && $seconds_since < 86400){
            $hours = round($seconds_since / 3600);
            if($hours == 1){
               return "1 hour ago";
            }
            else{
               return $hours.' hours ago';
            }
         }
         else if($seconds_since > 86400 && $seconds_since < 604800){
            $days = round($seconds_since/86400);
            if($days == 1){
               return "1 day ago";
            }
            else{
               return $days.' days ago';
            }
         }
         else if($seconds_since > 604799){
            $weeks = round($seconds_since/604800);
            if($weeks == 1){
               return "1 week ago";
            }
            else{
               return $weeks.' weeks ago';
            }
         }
      }

_________________
http://WayneWhitty.com
http://GTFONOOB.com

BBcode was invented because programmers were too lazy or afraid to deal with real HTML. So they invented their own markup.
Top Profile E-mail 

Offline nitromaster

Bebo Addict


Joined: Mon Mar 24, 2008 2:19 am

Posts: 960

PostPosted: Sat Sep 20, 2008 7:09 pm   Post subject: Re: PHP Time Function (Seconds,Minutes,Hours,Days,Weeks) since.   

Thanks

Saves me doing it myself :haha: :haha:
Top Profile E-mail 

Offline FireBird

Newbie


Joined: Mon Sep 29, 2008 10:36 am

Posts: 1

PostPosted: Mon Sep 29, 2008 11:18 am   Post subject: Re: PHP Time Function (Seconds,Minutes,Hours,Days,Weeks) since.   

Messiah-Wayne,

Interesting code. Please forgive my n00bness :n00b: , although I'm a web designer, I'm currently trying to learn and understand web programming. How would your code be used?

I'm running MAMP and I'm not sure how to execute this properly.

Thanks.
Top Profile E-mail 

Offline Messiah-Wayne

Gold Member


User avatar


Joined: Mon Jul 28, 2008 3:06 pm

Posts: 179

Location: Ireland

PostPosted: Sat Oct 04, 2008 7:54 pm   Post subject: Re: PHP Time Function (Seconds,Minutes,Hours,Days,Weeks) since.   

Hi Firebird...

Wow... you're using a MAMP server? Can I suggest a LAMP? Or even a WAMP? You do know that even Macintosh uses a LAMP don't you?

Anyway... All you have to do to use my code is give it a timestamp. This timestamp can be taken from a database or wherever it was recorded.

$time = mktime();
echo time_since($time);


That will then print the seconds, minutes, hours, days or weeks that have passed since.

_________________
http://WayneWhitty.com
http://GTFONOOB.com

BBcode was invented because programmers were too lazy or afraid to deal with real HTML. So they invented their own markup.
Top Profile E-mail 
Display posts from previous:  Sort by  
Reply to topic Page 1 of 1 [ 5 posts ]

 
 •  

Who is online
Users browsing this forum: No registered users and 1 guest

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