The time is expressed in the form of the double version of PHP and js. Not much nonsense. The PHPFORSmarty copy code is as follows: * Name: time_ago * Purpose: Use the timestamp to express the timestamp in the form of * Second within 1 minute * nonsense within 1 hour.
Php for Smarty
The code is as follows:
* Name: time_ago
* Purpose: specify the timestamp as a representation of the current time.
* Per second in 1 minute
* Display by minute within one hour
* Display on time within 1 day
* Displayed as yesterday in three days, the day before yesterday
* Display the specific date in 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. 'mine ';
}
$ Retrun. = 'pre ';
} Else if ($ time_deff> = 60 ){
$ Minute = intval ($ time_deff/60 );
$ Second = $ time_deff % 60;
$ Retrun = $ minute. 'Minute ';
If ($ second> 0 ){
$ Retrun. = $ second. 'second ';
}
$ Retrun. = 'pre ';
} Else {
$ Retrun = $ time_deff. 'seconds ago ';
}
Return $ retrun;
}
Javascript
JavaScript is a little more complex, divided into three function implementations
The code is as follows:
/**
* String filling
* @ Param string str string to be filled
* @ Param int len the length of the target string
* @ Param str chr the character used for filling is a space by default.
* @ Param str left | right | both: right by default
*/
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 the date
* Similar to the php Date function, a Unix timestamp (in seconds) is input to return the specified format.
* Format (case insensitive ):
* Y indicates a four-digit year.
* M indicates a two-digit month.
* D indicates a two-digit day.
* H indicates 2 bits
* I indicates two points
* S indicates 2 seconds
*/
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 + 'mine ';
}
Retrun + = 'pre ';
} Else if (time_deff> = 60 ){
Minute = parseInt ($ time_deff/60 );
Second = time_deff % 60;
Retrun = minute + 'minutes ';
If (second> 0 ){
Retrun + = second + 'second ';
}
Retrun + = 'pre ';
} Else {
Retrun = time_deff + 'seconds ago ';
}
Return retrun;
}
Bytes. The php for Smarty code is as follows: * Name: time_ago * Purpose: the timestamp is used to express the time stamp in the form of * seconds within 1 minute * within 1 hour...