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
Time Elapsed Function
Old 10-18-2007, 11:21 AM Time Elapsed Function
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
This is my time_elapsed function to show user friendly elapsed time for things instead of ugly date.

PHP Code:
function time_elapsed($timestamp$parse 0) {
if(
$parse == 1) {
$timestamp strtotime($timestamp);
}
$curTimestamp time();
$difference $curTimestamp $timestamp;
switch(
$difference) {
case (
$difference == 0):
return 
"A second ago!";
break;
case (
$difference <= 600):
$minutes round($difference 60, -0.5);
return 
"$minutes minute".(($minutes 1)?"s":"")." ago";
break;
case (
$difference <= 900):
return 
"A quarter ago";
break;
case (
$difference <= 1800):
return 
round($difference 60, -0.5)." minutes ago";
break;
case (
$difference <= 2700):
return 
"Half an hour ago";
break;
case (
$difference <= 3600):
return 
"Three quarters ago";
break;
case (
$difference <= 86400):
$minutes floor(($difference 3600) / 60);
$hours floor(($difference 86400) / 3600);
return 
floor($difference 3600)." hour" . (($hours 1)?"s":"").(($minutes 0)?" and $minutes minute".(($minutes 1)?"s":""):"") . " ago";
break;
case (
$difference 86400):
$hours floor(($difference 86400) / 3600);
$days floor($difference 86400);
return 
"$days day" . (($days 1)?"s":"").(($hours 0)?" and $hours hour".(($hours 1)?"s":""):"") . " ago";
break;
default:
return 
date("\A\\t H:i \o\\n d-m-Y",$timestamp);
}

To use (example):

PHP Code:

// Date/time from SQL

print time_elapsed($sqlrow['date_posted'], 1); // This will parse the sql date/time so it works correctly with the function

print time_elapsed($time_posted0); // This is if its already a timestamp 
__________________

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

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
 
Register now for full access!
Reply     « Reply to Time Elapsed Function
 

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.08141 seconds with 12 queries