Home Backend Development PHP Tutorial 将时间以距今多久的形式表示,PHP,js双版本_PHP

将时间以距今多久的形式表示,PHP,js双版本_PHP

Jun 01, 2016 pm 12:10 PM
time

废话不多说直接上代码。
PHP FOR Smarty
复制代码 代码如下:
* Name: time_ago
* Purpose: 将时间戳专为距当前时间的表现形式
* 1分钟内按秒
* 1小时内按分钟显示
* 1天内按时分显示
* 3天内以昨天,前天显示
* 超过3天显示具体日期
*
* @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 = "前天 " . date('H:i', $time);
} else if ($time_deff >= 86400) {
$retrun = "昨天" . date('H:i', $time);
} else if ($time_deff >= 3600) {
$hour = intval($time_deff / 3600);
$minute = intval(($time_deff % 3600) / 60);
$retrun = $hour . '小时';
if ($minute > 0) {
$retrun .= $minute . '分钟';
}
$retrun .= '前';
} else if ($time_deff >= 60) {
$minute = intval($time_deff / 60);
$second = $time_deff % 60;
$retrun = $minute . '分';
if ($second > 0) {
$retrun .= $second . '秒';
}
$retrun .= '前';
}else{
$retrun = $time_deff.'秒前';
}
return $retrun;
}

Javascript
JS比较复杂一点,分为3个函数实现
复制代码 代码如下:
/**
* 字符串填充
* @param string str 要进行填充的字符串
* @param int len 目标字符串长度
* @param str chr 用于填充的字符 默认为空格
* @param str dir 填充位置 left|right|both 默认为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 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;
}
/**
* 格式化日期
* 类似php Date函数,传入Unix 时间戳(秒级)返回指定格式
* 格式(不区分大小写):
* y 表示4位年份
* m 表示2位月份
* d 表示2位日
* h 表示2位时
* i 表示2位分
* s 表示2位秒
*/
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 = "前天 " + formatDate('H:i', time);
} else if (time_deff >= 86400) {
retrun = "昨天" + formatDate('H:i', time);
} else if (time_deff >= 3600) {
hour = parseInt(time_deff / 3600);
minute = parseInt((time_deff % 3600) / 60);
retrun = hour + '小时';
if (minute > 0) {
retrun += minute + '分钟';
}
retrun += '前';
} else if (time_deff >= 60) {
minute = parseInt($time_deff / 60);
second = time_deff % 60;
retrun = minute + '分';
if (second > 0) {
retrun += second + '秒';
}
retrun += '前';
}else{
retrun = time_deff +'秒前';
}
return retrun;
}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How much does a Douyin level 10 light sign cost? How many days does it take to create a level 10 fan sign? How much does a Douyin level 10 light sign cost? How many days does it take to create a level 10 fan sign? Mar 11, 2024 pm 05:37 PM

On the Douyin platform, many users are eager to obtain level certification, and the level 10 light sign shows the user's influence and recognition on Douyin. This article will delve into the price of Douyin’s level 10 light boards and the time it takes to reach this level to help users better understand the process. 1. How much does a level 10 Douyin light sign cost? The price of Douyin's 10-level light signs will vary depending on market fluctuations and supply and demand. The general price ranges from a few thousand yuan to ten thousand yuan. This price mainly includes the cost of the light sign itself and possible service fees. Users can purchase level 10 light signs through Douyin’s official channels or third-party service agencies, but they should pay attention to legal channels when purchasing to avoid false or fraudulent transactions. 2. How many days does it take to create a level 10 fan sign? Reach level 10 light sign

Can linux reset system time? Can linux reset system time? Mar 13, 2023 am 10:50 AM

Linux can reset the system time. The reset method is: 1. Use the date command to check the time; 2. Use the "yum install ntp" command to install ntp; 3. Use the "ntpdate -u ntp.api.bz" command to implement network time Just sync.

Why does my Go program take longer to compile? Why does my Go program take longer to compile? Jun 09, 2023 pm 06:00 PM

In recent years, Go language has become the choice of more and more developers. However, compared to other programming languages, the compilation speed of Go language is not fast enough. Many developers will encounter this problem when compiling Go programs: Why does my Go program take longer to compile? This article will explore this issue from several aspects. The compiler architecture of Go language The compiler architecture of Go language adopts a three-stage design, which are front-end, middle layer and back-end. The front-end is responsible for translating the source code into intermediate code in Go language, and the middle layer will

How long does it take to clear the Elden Ring? How long does it take to clear the Elden Ring? Mar 11, 2024 pm 12:50 PM

Players can experience the main plot of the game and collect game achievements when playing in Elden's Circle. Many players don't know how long it takes to clear Elden's Circle. The player's clearance process is 30 hours. How long does it take to clear the Elden Ring? Answer: 30 hours. 1. Although this 30-hour clearance time does not refer to a master-like speed pass, it also omits a lot of processes. 2. If you want to get a better game experience or experience the complete plot, then you will definitely need to spend more time on the duration. 3. If players collect them all, it will take about 100-120 hours. 4. If you only take the main line to brush BOSS, it will take about 50-60 hours. 5. If you want to experience it all: 150 hours of base time.

How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? Mar 24, 2024 pm 01:31 PM

Xiaohongshu, a platform full of life and knowledge sharing, allows more and more creators to express their opinions freely. In order to get more attention and likes on Xiaohongshu, in addition to the quality of content, the time of publishing works is also crucial. So, how to set the time for Xiaohongshu to publish works? 1. How to set the time for publishing works on Xiaohongshu? 1. Understand the active time of users. First, it is necessary to clarify the active time of Xiaohongshu users. Generally speaking, 8 pm to 10 pm and weekend afternoons are the times when user activity is high. However, this time period will also vary depending on factors such as audience group and geography. Therefore, in order to better grasp the active period of users, it is recommended to conduct a more detailed analysis of the behavioral habits of different groups. By understanding users’ lives

How to remove hours, minutes and seconds from time in php How to remove hours, minutes and seconds from time in php Mar 13, 2023 am 11:20 AM

How to use PHP to remove hours, minutes and seconds from time: 1. Create a PHP sample file; 2. Use the strtotime function to convert the date and time into a timestamp; 3. Use the date function to format the date or time to remove the hours, minutes and seconds.

Detailed explanation of Linux file time viewing techniques Detailed explanation of Linux file time viewing techniques Feb 21, 2024 pm 01:15 PM

Detailed explanation of Linux file time viewing techniques In Linux systems, file time information is very important for file management and tracking changes. The Linux system records file change information through three main time attributes, namely access time (atime), modification time (mtime) and change time (ctime). This article details how to view and manage this file time information, and provides specific code examples. 1. Check the file time information by using the ls command with the parameter -l to list the files.

How to adjust the time on gshock watch How to adjust the time on gshock watch Feb 21, 2024 pm 09:04 PM

Many users are not sure how to adjust the time of the gshock watch. The overall method is relatively simple. Here is a detailed introduction to the adjustment method of the gshock watch. Users who do not know how to operate can refer to it. How to adjust the time on a gshock watch 1. First label these buttons according to ABCD. A key: It can be understood as the exit key, usually used together with the C key. The B key and the D key can be understood as: plus and minus keys, which are the adjustment keys. For example, if it is 13 o'clock now, press the D key when it reaches 14 o'clock. The C key is the mode key. You can switch modes by pressing it. Generally, the mode key of a watch is in the lower left corner and has the word "mode" in English. Operation method 1. First we need to adjust the watch to the

See all articles