Skip to content

Instantly share code, notes, and snippets.

@navarr
Created May 3, 2010 18:13
Show Gist options
  • Select an option

  • Save navarr/388393 to your computer and use it in GitHub Desktop.

Select an option

Save navarr/388393 to your computer and use it in GitHub Desktop.
<?php
/* Notes:
- This is part of a much bigger "core" class for a website.
- ->make_time($unix_timestamp,$data_parameters) is pretty much date() but using the logged in user's timezone
*/
function fb_reltime($time,$html = true) // assumes a date in the past
{
if($time > TIME)
{ $future = 1; } else { $future = 0; }
$seconds = TIME - $time;
if($seconds < 5)
{
$reltime = "just now";
}
else if($seconds < 60) // Less than a Minute
{
if($seconds == 1) { $reltime = "{$seconds} second ago"; }
else { $reltime = "{$seconds} seconds ago"; }
}
else if($seconds <= 60*60) // Less than an Hour
{
$minutes = floor($seconds / 60);
if($minutes == 1) { $reltime = "{$minutes} minute ago"; }
else { $reltime = "{$minutes} minutes ago"; }
}
else if($this->make_time($time,"Y") < $this->make_time(TIME,"Y")) // More than a Year Ago
{
$reltime = $this->make_time($time,"F j, Y \a\\t g:ia");
}
else if((($this->make_time($time,"N") == ($this->make_time(TIME,"N")-1)) || ($this->make_time($time,"N") == 7 && $this->make_time(TIME,"N") == 1)) && $seconds < 60*60*48) // Yesterday
{
$reltime = "Yesterday at ".$this->make_time($time,"g:ia");
}
else if($seconds <= 60*60*24) // Less than a Day
{
$hours = floor(($seconds / 60) / 60);
if(($seconds / 60) / 60 < 1.5) { $reltime = "about an hour ago"; }
elseif($hours < 2) { $reltime = "over an hour ago"; }
else { $reltime = "{$hours} hours ago"; }
}
else if($seconds < 60*60*24*4) // Less than Four Days Ago
{
$reltime = $this->make_time($time,"D \a\\t g:ia");
}
else
{
$reltime = $this->make_time($time,"F j \a\\t g:ia"); // Four+ Days Ago, but Less than a Year Ago
}
if(!$html) { return $reltime; }
return '<abbr title="'.$this->make_time($time,"D, d M Y H:i:s O").'" data-epoch="'.$time.'" class="js-update-time">'.$reltime.'</abbr>';
}
function fb_reltime(seconds,html)
{
if(!html) { var html = false; }
var now_date = new Date();
var TIME = Math.floor((new Date()).getTime() / 1000);
//var seconds = Math.floor(time_ms / 1000);
var time_ms = seconds * 1000;
var date = new Date(time_ms);
var reltime;
seconds = TIME - seconds;
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var sdays= ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
if(seconds < 5) // Five or Less Seconds Ago
{
reltime = "just now";
}
else if(seconds < 60) // Less than a Minute Ago
{
reltime = seconds + " seconds ago";
}
else if(seconds <= 60*60) // Less than an Hour Ago
{
var minutes = Math.floor(seconds / 60);
if(minutes == 1) { reltime = "1 minute ago"; }
else { reltime = minutes + " minutes ago"; }
}
else if(date.getFullYear() < now_date.getFullYear()) // Over a Year Ago
{
var thr = tfhr_to_thr(date.getHours());
reltime = months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear() + " at " + thr[0] + ":" + ddigit(date.getMinutes()) + thr[1];
}
else if(((date.getDay() == now_date.getDay() - 1) || (date.getDay() == 6 && now_date.getDay() == 0)) && (seconds < 60*60*48)) // Yesterday
{
var thr = tfhr_to_thr(date.getHours());
reltime = "Yesterday at " + thr[0] + ":" + ddigit(date.getMinutes()) + thr[1];
}
else if(seconds <= 60*60*24) // Less than a Day Ago
{
var hours = Math.floor((seconds / 60) / 60);
if((seconds / 60) / 60 < 1.5) { reltime = "about an hour ago"; }
else if(hours < 2) { reltime = "over an hour ago"; }
else { reltime = hours + " hours ago"; }
}
else if(seconds < 60*60*24*4) // Less than Four Days Ago
{
var thr = tfhr_to_thr(date.getHours());
reltime = sdays[date.getDay()] + " at " + thr[0] + ":" + ddigit(date.getMinutes()) + thr[1];
}
else // Four+ Days Ago
{
var thr = tfhr_to_thr(date.getHours());
reltime = months[date.getMonth()] + " " + date.getDate() + " at " + thr[0] + ":" + ddigit(date.getMinutes()) + thr[1];
}
if(html)
{
return '<abbr title="' + sdays[date.getDay()] + ', ' + date.getDate() + " " + months[date.getMonth()] + " " + date.getFullYear() + ' ' + ddigit(date.getHours()) + ":" + ddigit(date.getMinutes()) + ":" + ddigit(date.getSeconds()) + ' ' + gettz(date) + '" data-epoch="' + Math.floor(date.getTime() / 1000) + '" class="js-update-time">' + reltime + '</abbr>';
}
return reltime;
}
function tfhr_to_thr(hours)
{
var ampm;
var valreturn;
if(hours < 24 && hours > 11) { ampm = "pm"; }
else if(hours < 12 || hours == 24) { ampm = "am"; }
if(hours == 0) { returnval = "12"; }
else if(hours > 12) { returnval = hours - 12; }
else { returnval = hours; }
return [returnval,ampm];
}
function ddigit(int)
{
if(int < 10) { return "0" + int; } else { return int; }
}
function gettz(date)
{
var offset = date.getTimezoneOffset();
var str;
if(offset < 1) { str = "+"; } else { str = "-"; }
str = str + ddigit(Math.floor(offset / 60));
str = str + ddigit(offset % 60);
return str;
}
function update_time()
{
for(var i = 0; i < document.getElementsByClassName("js-update-time").length; i++)
{
document.getElementsByClassName("js-update-time")[i].innerHTML = fb_reltime(document.getElementsByClassName("js-update-time")[i].getAttribute("data-epoch"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment