PHP Time Function (Seconds,Minutes,Hours,Days,Weeks) since.
| Author | Message |
|---|---|
|
Gold Member
Joined: Mon Jul 28, 2008 3:06 pm Posts: 179 Location: Ireland |
|
|
[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 |
|
|
Gold Member
Joined: Mon Jul 28, 2008 3:06 pm Posts: 179 Location: Ireland |
|
|
Sorry, I'm too used to PHP Freaks' PHP BBcode
Code: Select all function time_since($time){_________________ 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 |
|
|
Bebo Addict Joined: Mon Mar 24, 2008 2:19 am Posts: 960 |
|
|
Thanks
Saves me doing it myself ![]() |
|
| Top |
|
|
Newbie Joined: Mon Sep 29, 2008 10:36 am Posts: 1 |
|
|
Messiah-Wayne,
Interesting code. Please forgive my n00bness , 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 |
|
|
Gold Member
Joined: Mon Jul 28, 2008 3:06 pm Posts: 179 Location: Ireland |
|
|
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. Code: Select all $time = mktime();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 |
|
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


, although I'm a web designer, I'm currently trying to learn and understand web programming. How would your code be used?