Skip to content

Instantly share code, notes, and snippets.

@prashnts
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prashnts/59c3bc56e52afd233cee to your computer and use it in GitHub Desktop.
Save prashnts/59c3bc56e52afd233cee to your computer and use it in GitHub Desktop.
A PHP Time Ago implementation, which cares about Timestamps WAY back in time, and returns their actual dates.
<?php
/**
* Returns a Pretty Time Ago string from given Timestamp. Returns bare date if
* timestamp is way back in past.
* @example "posted".timeAgo(xx): "posted just now".
* "posted a few minutes ago"
* "posted 14 minutes ago"
* "posted today at 7:00PM"
* "posted yesterday at 7:00PM"
* "posted 7:00PM, Friday 29 August 2014"
* @param int $Timestamp The timestamp.
* @return string The Pretty string.
* @author Prashant Sinha <firstname,lastname>@outlook.com
* @see \time(), \date()
* @since v0.1 20140904
* @version v0.2
*/
function timeAgo($Timestamp) {
/**
* @internal Get Time Difference and then return the String.
*/
$Ago = time() - $Timestamp;
if ($Ago < 60) return "just now";
elseif ($Ago < 120) return "a few minutes ago";
elseif ($Ago < 3570) return round($Ago / 60)." minutes ago";
elseif ($Ago < 86400) return "today, at ".date('g:iA', $Timestamp);
elseif ($Ago < 172800) return "yesterday, at ".date('g:iA', $Timestamp);
else return date('g:iA, j F Y', $Timestamp);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment