Hello there!
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 for 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