Created
September 6, 2009 01:17
-
-
Save haqu/181590 to your computer and use it in GitHub Desktop.
relative date
This file contains 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
<? | |
# relative_date | |
# usage: relative_date("2009-09-09 9:09:09") => "9 minutes ago" | |
# just now, 25 seconds ago... yesterday, 5 days ago, 3 months ago | |
function pluralize($count,$word) { | |
if($count == 0) { | |
return "no ".$word."s"; | |
} else if($count == 1) { | |
return "1 ".$word; | |
} else { | |
return floor($count)." ".$word."s"; | |
} | |
} | |
function date_diff($interval, $date1, $date2) { | |
$date1 = strtotime($date1); | |
$date2 = strtotime($date2); | |
$seconds = $date2 - $date1; | |
if ($seconds < 0) { | |
$tmp = $date1; | |
$date1 = $date2; | |
$date2 = $tmp; | |
$seconds = 0-$seconds; | |
} | |
if ($interval =='y' || $interval=='m') { | |
$date1 = date("Y-m-d h:i:s", $date1); | |
$date2= date("Y-m-d h:i:s", $date2); | |
} | |
switch($interval) { | |
case "y": | |
list($year1, $month1, $day1) = split('-', $date1); | |
list($year2, $month2, $day2) = split('-', $date2); | |
$time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1)); | |
$time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2)); | |
$diff = $year2 - $year1; | |
if($month1 > $month2) { | |
$diff -= 1; | |
} elseif($month1 == $month2) { | |
if($day1 > $day2) { | |
$diff -= 1; | |
} elseif($day1 == $day2) { | |
if($time1 > $time2) { | |
$diff -= 1; | |
} | |
} | |
} | |
break; | |
case "m": | |
list($year1, $month1, $day1) = split('-', $date1); | |
list($year2, $month2, $day2) = split('-',$date2); | |
$time1 = (date('H',$date1)*3600) + (date('i',$date1)*60) + (date('s',$date1)); | |
$time2 = (date('H',$date2)*3600) + (date('i',$date2)*60) + (date('s',$date2)); | |
$diff = ($year2 * 12 + $month2) - ($year1 * 12 + $month1); | |
if($day1 > $day2) { | |
$diff -= 1; | |
} elseif($day1 == $day2) { | |
if($time1 > $time2) { | |
$diff -= 1; | |
} | |
} | |
break; | |
case "w": | |
$diff = floor($seconds / 604800); | |
break; | |
case "d": | |
$diff = floor($seconds / 86400); | |
break; | |
case "h": | |
$diff = floor($seconds / 3600); | |
break; | |
case "i": | |
$diff = floor($seconds / 60); | |
break; | |
case "s": | |
$diff = $seconds; | |
break; | |
} | |
return $diff; | |
} | |
function relative_date($date_added) { | |
$now = date('Y-m-d H:i:s'); | |
$hours_ago = date_diff('h',$now,$date_added); | |
if($hours_ago<1) { | |
$min_ago = date_diff('i',$now,$date_added); | |
if($min_ago < 1) { | |
$sec_ago = date_diff('s',$now,$date_added); | |
if($sec_ago < 1) { | |
$ago = "just now"; | |
} else { | |
$ago = pluralize($sec_ago,"second")." ago"; | |
} | |
} else { | |
$ago = pluralize($min_ago,"minute")." ago"; | |
} | |
} elseif($hours_ago>743) { | |
$months_ago = date_diff('m',$now,$date_added); | |
$ago = pluralize($months_ago,"month")." ago"; | |
} elseif($hours_ago>23) { | |
$days_ago = date_diff('d',$now,$date_added); | |
if($days_ago == 1) { | |
$ago = "yesterday"; | |
} else { | |
$ago = $days_ago." days ago"; | |
} | |
} else { | |
$ago = pluralize($hours_ago,"hour")." ago"; | |
} | |
return $ago; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment