Qondio
Front
Intel
IntelMart
Shares
My Qondio
Account
Sava > Intel > Display the time passed in any format you want

qondio.com/G5OY PRINT EMAIL

Display the time passed in any format you want

By Sava Stefan of Sava's Place

If you use digg you probably noticed that they displayed the time passed since a comment has been posted like it shows in the attached image to this article.

To obtain a rezult in this format we will use the function strtotime. This function counts the seconds that have passed since the 1st of January 1970.
In order to obtain the number of seconds exactly since a comment has been posted we need to save the date for each comment in the following format: date('Y-m-d H:i:s') . This will return 2008-11-12 14:39:53.

So when you create the date field in the MySQL table choose as type datetime.

Let's start off with inserting a comment in the database.

INSERT INTO `comments` (`name` , `comment_text`, `date`) VALUES ('Sava Stefan', 'This is a test comment', NOW( ));

NOW() will insert the current date and time at the exact moment the query is executed. Eg: 2008-11-12 14:39:53.

It's now time to show the time passed since a comment has been posted.

Now let's create a function called countTime.

function countTime($comment_date) {
$now = date('Y-m-d H:i:s');
$number_of_seconds = (strtotime($now) - strtotime($comment_date));
}

Until now I managed to get the number of seconds passed since the comment has been posted ($comment_date). We go on further and obtain the number of hours and minutes with simple mathematics:


$number_of_minutes = $number_of_seconds / (60); $number_of_hours = $number_of_minutes / (60); $number_of_hours_to_display = intval($number_of_hours); $number_of_minutes_to_display = intval($number_of_minutes - ($number_of_hours_to_display * 60));

As you can see the number of minutes passed is equal to the number of seconds obtained earlier (that first diference - $number_of_seconds ) split to 60. The number of hours will also be equal to the number of minutes split to 60 as well.
I used intval to obtain the integer value so we don't display anything like: 1,523 hours Let's not forget to get the integer for the number of seconds as well:


$number_of_seconds_to_display= intval($number_of_seconds - ($number_of_minutes_to_display * 60 * 60) - ($number_of_minutes_to_display * 60));

In case you don't get this last line try looking at the clock and notice how many seconds an hour and a minute have. Let's customize the output of the function:


echo 'This comment was posted '.$number_of_hours_to_display.' hours, '.$number_of_minutes_to_display.' minutes and '.$number_of_seconds_to_display.' seconds ago.';

The complete function


function countTime($comment_date) {
$now = date('Y-m-d H:i:s');
$number_of_seconds = (strtotime($now) - strtotime($comment_date));
$number_of_minutes = $number_of_seconds / (60);
$number_of_hours = $number_of_minutes / (60);
$number_of_hours_to_display = intval($number_of_hours);
$number_of_minutes_to_display = intval($number_of_minutes - ($number_of_hours_to_display * 60));
$number_of_seconds_to_display= intval($number_of_seconds - ($number_of_minutes_to_display * 60 * 60) - ($number_of_minutes_to_display * 60));
echo 'This comment was posted '.$number_of_hours_to_display.' hours, '.$number_of_minutes_to_display.' minutes and '.$number_of_seconds_to_display.' seconds ago.';
}

Integrating the function is simple as hell. If you obtained your comments as an array let's say that $comment['date'] is the date value of a comment from your database.


Name: $comment['name'];
Comment: $comment['comment_text'];
countTime($comment['date']);

It will display the following considering the database insert from above:


Name: Sava Stefan
Comment: This is a test comment
This comment was posted 3 hours, 24 minutes and 13 seconds ago.

Of course you can calculate the time left until a certain thing should happen:


$number_of_seconds_until = (strtotime($data_in_future) - strtotime($now));

Get going ... you time machine

Images

Digg Comment ScreenShot
Digg Comment ScreenShot

Contributed by Sava on March 17, 2009, at 11:47 AM UTC.

PLEASE VISIT THE CONTRIBUTOR'S WEBSITE
Sava's Place
Free PHP Scripts and CSS Templates
savasplace.com

Reactions

No reactions yet.

Rate This Intel

Please login or sign up to rate this intel.

Comments

Please login or sign up to add a comment.

Share

Copyright Notice

The copyright for this content entitled "Display the time passed in any format you want " has been specified by the contributor as:

Creative Commons Attribution 3.0 Details

This content may be copied, distributed, or modified as long as the original author is acknowledged with a link back to the content page. If you use this content according to the license specified, you must link to the following URL:

http://sava.qondio.com/

Login Here with
Any Email Address
Any Password
No account? Sign up.

Intel Contributor
This intel was contributed by Sava


Sava

Qondio Archive
May, 2012
123456
78910111213
14151617181920
21222324252627
28293031


2008
January, February, March, April, May, June, July, August, September, October, November, December
2009
January, February, March, April, May, June, July, August, September, October, November, December
2010
January, February, March, April, May, June, July, August, September, October, November, December
2011
January, February, March, April, May, June, July, August, September, October, November, December
2012
January, February, March, April, May

Sign Up
Not a member yet? Qondio is a powerful network for making it online. If you have a website to promote, we can help. Sign up and get in on the action.

About Qondio
Welcome to Qondio! Discover the awesome power this network can deliver by going to our About page. Or you could skip straight to the Sign Up form.

ABOUT
SUCCESS GUIDE
FEATURES
FAQ
ADVERTISE
CONTACT
USAGE POLICY
PRIVACY POLICY


TWITTER
FACEBOOK