I have a php function to do fuzzy time (aka time ago).
This is used when building a table from the server side, however now we are adding new items to the table through JavaScript, and we have the ability to select a date, therefore I need to duplicate the functionality in Javascript but have it accept the date in the format YYYY-MM-DD
e.g. 2012-12-14
.
I shall begin working on it, but I am terrible with dates in Javascript so have posted it here incase someone can do it faster.
The function is below:
/**
* Convert date into a 'fuzzy' format
* - 15 minutes ago, 3 days ago, etc.
* Pass a unix timestamp or a string to parse to a date.
* @param string|number
* @return string
*/
function fuzzyTime($date_from, $invalid_date = 'a long time ago') {
$_time_formats = array(
array(60, 'just now'),
array(90, '1 minute'),
array(3600, 'minutes', 60),
array(5400, '1 hour'),
array(86400, 'hours', 3600),
array(129600, '1 day'),
array(604800, 'days', 86400),
array(907200, '1 week'),
array(2628000, 'weeks', 604800),
array(3942000, '1 month'),
array(31536000, 'months', 2628000),
array(47304000, '1 year'),
array(3153600000, 'years', 31536000),
);
$now = time(); // current unix timestamp
// if a number is passed assume it is a unix time stamp
// if string is passed try and parse it to unix time stamp
if (is_numeric($date_from)) {
$dateFrom = $date_from;
} elseif (is_string($date_from)) {
$dateFrom = strtotime($date_from);
}
$difference = $now - $dateFrom; // difference between now and the passed time.
$val = ''; // value to return
if ($dateFrom <= 0) {
$val = $invalid_date;
} else {
//loop through each format measurement in array
foreach ($_time_formats as $format) {
// if the difference from now and passed time is less than first option in format measurment
if ($difference < $format[0]) {
//if the format array item has no calculation value
if (count($format) == 2) {
$val = $format[1] . ($format[0] === 60 ? '' : ' ago');
break;
} else {
// divide difference by format item value to get number of units
$val = ceil($difference / $format[2]) . ' ' . $format[1] . ' ago';
break;
}
}
}
}
return $val;
}
This is what I have so far, but it returns 83 years ago
var date = '2012-11-14';
console.log(fuzzyTime(date));
function fuzzyTime($date_from, $invalid_date) {
if($invalid_date === undefined){
$invalid_date = 'a long time ago';
}
var $_time_formats = [
[60, 'just now'],
[90, '1 minute'],
[3600, 'minutes', 60],
[5400, '1 hour'],
[86400, 'hours', 3600],
[129600, '1 day'],
[604800, 'days', 86400],
[907200, '1 week'],
[2628000, 'weeks', 604800],
[3942000, '1 month'],
[31536000, 'months', 2628000],
[47304000, '1 year'],
[3153600000, 'years', 31536000]
];
var $now = new Date(); // current unix timestamp
var $dateFrom = new Date($date_from);
var $difference = Math.abs(new Date() - $dateFrom);
var $val = ''; // value to return
if ($dateFrom <= 0) {
$val = $invalid_date;
} else {
for (var i = 0; i < $_time_formats.length; i++) {
var $format = $_time_formats[i];
if ($difference < $format[0]) {
//if the format array item has no calculation value
if ($format.length == 2) {
$val = $format[1] + ($format[0] === 60 ? '' : ' ago');
break;
} else {
// divide difference by format item value to get number of units
$val = Math.ceil($difference / $format[2]) + ' ' + $format[1] + ' ago';
break;
}
}
}
}
return $val;
}