Some nifty PHP date/time functions

March 8, 2008
I wrote two date/time functions in PHP that I was really pleased with so thought I'd post them.
function friendlydate($input) {
 if (!is_numeric($input)) $input = strtotime($input);
 if (date("Ymd", $input) == date("Ymd", time())) return "Today";
 if (date("Ymd", $input) == date("Ymd", time()-60*60*24)) return "Yesterday";
 if ($input > time()-60*60*24*6) return date("l", $input);
 if (date("Y", $input) == date("Y", time())) return date("F jS", $input);
 return date("F jS, Y", $input);
 }
function howlong($a=0, $b=0) {
 if (!is_numeric($a)) $a = strtotime($a); if (!is_numeric($b)) $b = strtotime($b);
 if ($a == 0) $a = time(); if ($b == 0) $b = time();
 if ($a == $b) return "at the same time";
 if ($a > $b) { $aa = $b; $b = $a; $a = $aa; $q = "away"; } else { $q = "ago"; }
 $value = $b-$a;
 if ($value == 1) return "1 second $q";
 if ($value < 60) return "$value seconds $q";
 $value /= 60;
 if (round($value) == 1) return "1 minute $q";
 if ($value < 60) return round($value) . " minutes $q";
 $value /= 60;
 if (round($value) == 1) return "1 hour $q";
 if ($value < 24) return round($value) . " hours $q";
 $value /= 24;
 if (round($value) == 1) if ($q == "ago") return "yesterday"; else return "tomorrow";
 if ($value < 31) return round($value) . " days $q";
 $value /= 31;
 if (round($value) == 1) return "1 month $q";
 if ($value < 12) return round($value) . " months $q";
 $value /= 12;
 if (round($value) == 1) return "1 year $q";
 return round($value) . " years $q";
 }
Name: Email: (Required only if you want a reply.) Comment: