PHP Classes

File: javascript/pretty_date.js

Recommend this page to a friend!
  Classes of Kabir Hossain   PHP CodeIgniter Tips Tricks   javascript/pretty_date.js   Download  
File: javascript/pretty_date.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: PHP CodeIgniter Tips Tricks
Collection of tips and examples to use CodeIgniter
Author: By
Last change:
Date: 1 month ago
Size: 5,172 bytes
 

Contents

Class file image Download
function prettyLinks(){ var links = document.getElementsByTagName("a"); for ( var i = 0; i < links.length; i++ ) if ( links[i].title ) { var date = prettyDate(links[i].title); if ( date ) links[i].innerHTML = date; } } prettyLinks(); setInterval(prettyLinks, 5000); /* * JavaScript Pretty Date * Copyright (c) 2011 John Resig (ejohn.org) * Licensed under the MIT and GPL licenses. */ // Takes an ISO time and returns a string representing how // long ago the date represents. function prettyDate(time){ var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")), diff = (((new Date()).getTime() - date.getTime()) / 1000), day_diff = Math.floor(diff / 86400); if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 ) return; return day_diff == 0 && ( diff < 60 && "just now" || diff < 120 && "1 minute ago" || diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" || diff < 7200 && "1 hour ago" || diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") || day_diff == 1 && "Yesterday" || day_diff < 7 && day_diff + " days ago" || day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago"; } // If jQuery is included in the page, adds a jQuery plugin to handle it as well if ( typeof jQuery != "undefined" ) jQuery.fn.prettyDate = function(){ return this.each(function(){ var date = prettyDate(this.title); if ( date ) jQuery(this).text( date ); }); }; $("a").prettyDate(); setInterval(function(){ $("a").prettyDate(); }, 5000); function pretty_date(date_str) { var time = ('' + date_str).replace(/-/g, "/").replace(/[TZ]/g, " "); var seconds = (new Date - new Date(time)) / 1000; var token = 'ago', list_choice = 1; if (seconds < 0) { seconds = Math.abs(seconds); token = 'from now'; list_choice = 2; } var i = 0, format; while (format = time_formats[i++]) if (seconds < format[0]) { if (typeof format[2] == 'string') return format[list_choice]; else return Math.floor(seconds / format[2]) + ' ' + format[1] + ' ' + token; } return time; }; var time_formats = [ [60, 'just now', 1], // 60 [120, '1 minute ago', '1 minute from now'], // 60*2 [3600, 'minutes', 60], // 60*60, 60 [7200, '1 hour ago', '1 hour from now'], // 60*60*2 [86400, 'hours', 3600], // 60*60*24, 60*60 [172800, 'yesterday', 'tomorrow'], // 60*60*24*2 [604800, 'days', 86400], // 60*60*24*7, 60*60*24 [1209600, 'last week', 'next week'], // 60*60*24*7*4*2 [2419200, 'weeks', 604800], // 60*60*24*7*4, 60*60*24*7 [4838400, 'last month', 'next month'], // 60*60*24*7*4*2 [29030400, 'months', 2419200], // 60*60*24*7*4*12, 60*60*24*7*4 [58060800, 'last year', 'next year'], // 60*60*24*7*4*12*2 [2903040000, 'years', 29030400], // 60*60*24*7*4*12*100, 60*60*24*7*4*12 [5806080000, 'last century', 'next century'], // 60*60*24*7*4*12*100*2 [58060800000, 'centuries', 2903040000] // 60*60*24*7*4*12*100*20, 60*60*24*7*4*12*100 ]; /* * Javascript Humane Dates * Copyright (c) 2008 Dean Landolt (deanlandolt.com) * Re-write by Zach Leatherman (zachleat.com) * * Adopted from the John Resig's pretty.js * at http://ejohn.org/blog/javascript-pretty-date * and henrah's proposed modification * at http://ejohn.org/blog/javascript-pretty-date/#comment-297458 * * Licensed under the MIT license. */ function humane_date(date_str){ var time_formats = [ [60, 'Just Now'], [90, '1 Minute'], // 60*1.5 [3600, 'Minutes', 60], // 60*60, 60 [5400, '1 Hour'], // 60*60*1.5 [86400, 'Hours', 3600], // 60*60*24, 60*60 [129600, '1 Day'], // 60*60*24*1.5 [604800, 'Days', 86400], // 60*60*24*7, 60*60*24 [907200, '1 Week'], // 60*60*24*7*1.5 [2628000, 'Weeks', 604800], // 60*60*24*(365/12), 60*60*24*7 [3942000, '1 Month'], // 60*60*24*(365/12)*1.5 [31536000, 'Months', 2628000], // 60*60*24*365, 60*60*24*(365/12) [47304000, '1 Year'], // 60*60*24*365*1.5 [3153600000, 'Years', 31536000], // 60*60*24*365*100, 60*60*24*365 [4730400000, '1 Century'], // 60*60*24*365*100*1.5 ]; var time = ('' + date_str).replace(/-/g,"/").replace(/[TZ]/g," "), dt = new Date, seconds = ((dt - new Date(time) + (dt.getTimezoneOffset() * 60000)) / 1000), token = ' Ago', i = 0, format; if (seconds < 0) { seconds = Math.abs(seconds); token = ''; } while (format = time_formats[i++]) { if (seconds < format[0]) { if (format.length == 2) { return format[1] + (i > 1 ? token : ''); // Conditional so we don't return Just Now Ago } else { return Math.round(seconds / format[2]) + ' ' + format[1] + (i > 1 ? token : ''); } } } // overflow for centuries if(seconds > 4730400000) return Math.round(seconds / 4730400000) + ' Centuries' + token; return date_str; }; if(typeof jQuery != 'undefined') { jQuery.fn.humane_dates = function(){ return this.each(function(){ var date = humane_date(this.title); if(date && jQuery(this).text() != date) // don't modify the dom if we don't have to jQuery(this).text(date); }); }; }