sea_1987
sea_1987

Reputation: 2944

Showing time elapsed with php

I am wanting to show the time elapsed since the time posted for a blog or similiar, I am using php and I am storing the time posted as a unix timestamp but I cannot for the live of me work out how to show the time since it was posted using the stored timestamp.

Upvotes: 4

Views: 7047

Answers (2)

XViD
XViD

Reputation: 306

If for example your posted timestamp is:

1289735677

You can also get the current timestamp by using time(), for example.

Then you can take your posted timestamp from the current one:

time() - 1289735677

In that way you will get the seconds elapsed. Now you can change them to human readable format for example.

See examples on how you can convert seconds to human readable format:

  1. http://www.phpro.org/examples/Convert-Seconds-To-Hours-Minutes-Seconds-Words.html

  2. http://www.laughing-buddha.net/php/lib/sec2hms/

Upvotes: 6

Russell Dias
Russell Dias

Reputation: 73292

If your PHP is >= 5.3.0 you can use the DateTime::diff method.

Upvotes: 2

Related Questions