Making a PHP/MySQL Timestamp look more attractive
So basically, I'm currently selecting a Timestamp from my MySQL database. In the database, the timestamp looks like so:
2010-06-30 12:36:08
Obviously for a webapp, that's not very attractive for users to view. So, using some CodeIgniter functions, I made it look a bit nicer.
<h4 class="timestamp">
<?php // Quickly calculate the timespan
$post_date = mysql_to_unix($row->date);
$now = time();
echo timespan($post_date, $now);?> ago
</h4>
If you don't do CodeIgniter, everything's standard PHP except 开发者_如何学Pythonfor echo timespan()
. CodeIgniter just echoes it as an 'English' timespan. So, an example output would be:
2 Months, 4 Weeks, 5 Hours, 20 Minutes ago
That's all well and good, but, I want to make it seem nicer still... there are too many commas and its all a tad too long (I know, I'm picky...). What I'd like is:
- If the timespan is less than a day old, the output should be
7 hours, 33 minutes ago
- If the timespan is less than a week old, the output should be
4 days ago
- If the timespan is less than a month old, the output should be
2 weeks, 6 days ago
- If the timespan is over a month old, the output should be
4 months ago
- In the eventual case of the timespan being over a year old, the output should be
Over a year ago
As you can see, I'm currently using a CodeIgniter function to simplify this - but if there's any native PHP function that can do what I want it to, that'll be awesome.
Thanks for your help!
Jack
This is best done on the client side in the presentation layer. Here is a JS solution:
Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
Timeago will turn all abbr elements with a class
of timeago and an ISO 8601 timestamp in the title
:
<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>
into something like this:
<abbr class="timeago" title="July 17, 2008">about a year ago</abbr>
To convert the date into the ISO 8601 format you can do something like this:
<?= date("c", $post_date) ?>
Examples:
You opened this page less than a minute ago. (This will update every minute. Wait for it.)
This page was last modified 11 days ago.
Ryan was born 31 years ago.
$ts = new DateTime();
$ts->setTimestamp($my_timestamp);
$cur = new DateTime();
$difference = $cur->diff($ts);
if ($difference->format("%a") == 0)
$out = $difference->format("%h hours %i minutes");
elseif ($difference->format("%a") < 7)
$out = $difference->format("%a days");
elseif ($difference->format("%m") == 0) {
$days = $difference->format("%a");
$out = sprintf("%d weeks %d days", floor($days / 7),
$days % 7);
}
elseif ($difference->format("%y") == 0)
$out = $difference->format("%m months");
else
$out = "over a year";
You'll have to make a few adjustments if you don't want stuff like "1 days".
精彩评论