In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use PHP and js to express the time in the form of how long ago. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
PHP FOR Smarty
The copy code is as follows:
* Name: time_ago
* Purpose: tailor the timestamp to the representation from the current time
* by second within 1 minute
* display by minute within 1 hour
* display on time within 1 day
* within 3 days to show yesterday and the day before yesterday
* display a 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 Hridged, $time)
} else if ($time_deff > = 172800) {
$retrun = "day before yesterday". Date ('Hazzieu, $time)
} else if ($time_deff > = 86400) {
$retrun = yesterday. Date ('Hazzieu, $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. 'minutes'
}
$retrun. = 'front'
} else if ($time_deff > = 60) {
$minute = intval ($time_deff / 60)
$second = $time_deff 60
$retrun = $minute. 'Fen'
If ($second > 0) {
Retrun. = $second. 'seconds'
}
$retrun. = 'front'
} else {
$retrun = $time_deff.' Seconds ago.
}
Return $retrun
}
Javascript
JS is a bit more complex, which is divided into three function implementations.
The copy code is as follows:
/ * *
* string padding
* @ param string str string to be populated
* @ param int len target string length
* @ param str chr the characters used to fill are spaces by default
* @ param str dir fill 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 php Date function, input Unix timestamp (seconds) to return the specified format
* format (case-insensitive):
* y represents a 4-digit year
* m represents a 2-digit month
* d represents a 2-digit day
* h means 2 digits
* I means 2 digits
* s means 2-bit 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 (/ ybinGil year)
Format = format.replace (/ mbingijimonmonth)
Format = format.replace (/ dbingidyday)
Format = format.replace (/ hmp gijjol hour)
Format = format.replace (/ iBandgijime)
Format = format.replace (/ scompgimme 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 hijack, time)
} else if (time_deff > = 172800) {
Retrun = "the day before yesterday" + formatDate ('Hizujah, time)
} else if (time_deff > = 86400) {
Retrun = "yesterday" + formatDate ('Havana, 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 + 'minutes'
}
Retrun + = 'front'
} else if (time_deff > = 60) {
Minute = parseInt ($time_deff / 60)
Second = time_deff 60
Retrun = minute + 'min'
If (second > 0) {
Retrun + = second + 'seconds'
}
Retrun + = 'front'
} else {
Retrun = time_deff + 'seconds ago'
}
Return retrun
}
This is the end of the article on "how to express time in the form of PHP and js". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.