The time is expressed in the form of the two versions of PHP and js.

Source: Internet
Author: User
Still starting from the project requirements, some of this function, PHP version has written a Smarty plug-in, as long as a simple modification of the function name can be directly called in PHP, at the same time, in order to display the time in the JSON data obtained during AJAX paging in the same way, I wrote another JS version.
Php for Smarty
Copy codeThe 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
Copy codeThe 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.