Without further ado, let’s get straight to the code.
PHP FOR Smarty
Copy code The code is as follows:
* Name: time_ago
* Purpose: Change the time The stamp is designed to express the current time
* Displayed in seconds within 1 minute
* Displayed in minutes within 1 hour
* Displayed in hours and minutes within 1 day
* Displayed in yesterday and the day before yesterday within 3 days
* Display specific date more than 3 days
*
* @author Peter Pan
* @param int $time input int
*/
function smarty_modifier_time_ago($time) {
$ time_deff = time() - $time;
$retrun = '';
if ($time_deff >= 259200) {
$retrun = date('Y-m-d H:i', $time);
} else if ($time_deff >= 172800) {
$retrun = "the day before yesterday" . date('H:i', $time);
} else if ($time_deff >= 86400 ) {
$retrun = "Yesterday" . date('H:i', $time);
} else if ($time_deff >= 3600) {
$hour = intval($time_deff / 3600);
$minute = intval(($time_deff % 3600) / 60);
$retrun = $hour . 'hour';
if ($minute > 0) {
$ retrun .= $minute . 'minute';
}
$retrun .= 'before';
} else if ($time_deff >= 60) {
$minute = intval($time_deff / 60);
$second = $time_deff % 60;
$retrun = $minute . 'minute';
if ($second > 0) {
$retrun .= $second . 'seconds';
}
$retrun .= 'before';
}else{
$retrun = $time_deff.'seconds ago';
}
return $retrun;
}
Javascript
JS is a bit more complicated, divided into 3 functions to implement
Copy code The code is as follows :
/**
* String padding
* @param string str String to be padded
* @param int len Target string length
* @param str chr The character used for padding defaults to space
* @param str dir filling position left|right|both defaults to right
*/
function strPad(str, len, chr, dir){
str = str.toString();
len = ( typeof len == 'number') ? len : 0;
chr = (typeof chr == 'string') ? chr : ' ';
dir = (/left|right|both/i).test (dir) ? dir : 'right';
var repeat = function(c, l) {
var repeat = '';
while (repeat.length < l) {
repeat + = c;
}
return repeat.substr(0, l);
}
var diff = len - str.length;
if (diff > 0) {
switch (dir) {
case 'left':
str = '' + repeat(chr, diff) + str;
break;
case 'both':
var half = repeat (chr, Math.ceil(diff / 2));
str = (half + str + half).substr(1, len);
break;
default:
str = '' + str + repeat(chr, diff);
}
}
return str;
}
/**
* Format date
* Similar to the php Date function, pass in the Unix timestamp (second level) and return the specified format
* Format (not case sensitive):
* y represents the 4-digit year
* m represents the 2-digit month
* d represents the 2-digit day
* h represents the 2-digit hour
* i represents the 2-digit minute
* s represents the 2-digit second
*/
function formatDate(format,timestamp) {
var date = new Date(parseInt(timestamp) * 1000);
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate ();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
month = strPad(month,2, '0','left');
day = strPad(day,2,'0','left');
hour = strPad(hour,2,'0','left');
minute = strPad(minute,2,'0','left');
second = strPad(second,2,'0','left');
format = format.replace(/y /gi,year);
format = format.replace(/m/gi,month);
format = format.replace(/d/gi,day);
format = format.replace(/ h/gi,hour);
format = format.replace(/i/gi,minute);
format = format.replace(/s/gi,second);
return format;
}
function timeAgo(time) {
var nowTime = Date.parse(new Date()) / 1000;
var time_deff = nowTime - time;
retrun = '';
if (time_deff >= 259200) {
retrun = formatDate('Y-m-d H:i', time);
} else if (time_deff >= 172800) {
retrun = "the day before yesterday" + formatDate( 'H:i', time);
} else if (time_deff >= 86400) {
retrun = "Yesterday" + formatDate('H:i', time);
} else if ( time_deff >= 3600) {
hour = parseInt(time_deff / 3600);
minute = parseInt((time_deff % 3600) / 60);
retrun = hour + 'hour';
if (minute > 0) {
retrun += minute + 'minute';
}
retrun += 'before';
} else if (time_deff >= 60) {
minute = parseInt($time_deff / 60);
second = time_deff % 60;
retrun = minute + 'minute';
if (second > 0) {
retrun += second + ' seconds';
}
retrun += 'before';
}else{
retrun = time_deff +'seconds ago';
}
return retrun;
}
http://www.bkjia.com/PHPjc/325930.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325930.htmlTechArticleLet’s get straight to the code without further ado. PHP FOR Smarty Copy code code is as follows: * Name: time_ago * Purpose: Set the timestamp as the representation of the current time * in seconds within 1 minute * within 1 hour...