Skip to content

Instantly share code, notes, and snippets.

@brianherbert
Created January 18, 2011 21:28
Show Gist options
  • Save brianherbert/785189 to your computer and use it in GitHub Desktop.
Save brianherbert/785189 to your computer and use it in GitHub Desktop.
This is a function that returns a string stating the amount of time has passed from one timestamp to another.
<?php
function daysago($now,$then){
$daycount = floor(($now-$then)/86400); //86400 seconds in a day
$hourcount = floor(($now-$then)/3600); //3600 seconds in an hour
$minutecount = floor(($now-$then)/60); //60 seconds in a minute
if($daycount == 0) {
$str = 'Created ';
if($hourcount != 0) {
$str .= $hourcount.' ';
if($hourcount == 1) {
$str .= 'hour';
}else{
$str .= 'hours';
}
$str .= ' and ';
}
$str .= $minutecount.' ';
if($minutecount == 1) {
$str .= 'minute';
}else{
$str .= 'minutes';
}
$str .= ' ago.';
return $str;
}
if($daycount == 1) {
return 'Created '.$daycount.' day ago.';
}
return 'Created '.$daycount.' days ago.';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment