Created
May 19, 2013 23:12
A function that returns a "human-readable" time such as 8 hours ago or 1 month ago.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return a "human-readable" time such as 8 hours ago. | |
* | |
* @param string $time such as: 2013-03-18 21:13:59 | |
*/ | |
function time_since($time) | |
{ | |
$date = new \DateTime(); | |
$date->setTimestamp(strtotime($time)); | |
$interval = $date->diff(new \DateTime('now')); | |
if(!$interval->i && !$interval->h && !$interval->d && !$interval->m && !$interval->y) { | |
if($interval->s != "1") { | |
$plural = "s"; | |
return $interval->format('%s second'.$plural.' ago'); | |
} else { | |
return $interval->format('%s second ago'); | |
} | |
} else if(!$interval->h && !$interval->d && !$interval->m && !$interval->y) { | |
if($interval->i != "1") { | |
$plural = "s"; | |
return $interval->format('%i minute'.$plural.' ago'); | |
} else { | |
return $interval->format('%i minute ago'); | |
} | |
} else if(!$interval->d && !$interval->m && !$interval->y) { | |
if($interval->h != "1") { | |
$plural = "s"; | |
return $interval->format('%h hour'.$plural.' ago'); | |
} else { | |
return $interval->format('%h hour ago'); | |
} | |
} else if(!$interval->m && !$interval->y) { | |
if($interval->d != "1") { | |
$plural = "s"; | |
return $interval->format('%d day'.$plural.' ago'); | |
} else { | |
return $interval->format('%d day ago'); | |
} | |
} else if(!$interval->y) { | |
if($interval->m != "1") { | |
$plural = "s"; | |
return $interval->format('%m month'.$plural.' ago'); | |
} else { | |
return $interval->format('%m month ago'); | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Much better version by Ethan Taubman:
https://gist.github.com/etaubman/5609636