Table of Contents
php formatted timestamp display friendly time implementation ideas and codes, php ideas
Code to convert PHP time to timestamp
The php date function can format the timestamp into 2012-5-3
Home Backend Development PHP Tutorial PHP formatted timestamp display friendly time implementation ideas and codes, PHP ideas_PHP tutorial

PHP formatted timestamp display friendly time implementation ideas and codes, PHP ideas_PHP tutorial

Jul 13, 2016 am 10:16 AM
date function Timestamp format

php formatted timestamp display friendly time implementation ideas and codes, php ideas

The time in the project is always displayed as 2014-10-20 10:22, which seems very dull. On websites such as Weibo and QQ Space, the time is usually displayed as a few seconds ago, a few minutes ago, a few hours ago, etc. that are easy to read. We call this a friendly time format. So how to implement it using php?

The general idea is as follows:

If it is a New Year's Eve and it is more than 3 days, it will be displayed as the specific time

If it’s today

If it is within one minute, it will show how many seconds ago it was

If it is within one hour, it will display a few minutes ago

If it is the current day and is greater than one hour, it will be displayed as a few hours ago

If it is yesterday, the time will be displayed as yesterday

If it is the day before yesterday, it will display the time the day before yesterday

If it is more than three days (no New Year span), it will display the day of the month

Based on the above ideas, it is not difficult to write the implementation code:

The implementation code is as follows:

//格式化友好显示时间
function formatTime($time){
$now=time();
$day=date('Y-m-d',$time);
$today=date('Y-m-d');

$dayArr=explode('-',$day);
$todayArr=explode('-',$today);

//距离的天数,这种方法超过30天则不一定准确,但是30天内是准确的,因为一个月可能是30天也可能是31天
$days=($todayArr[0]-$dayArr[0])*365+(($todayArr[1]-$dayArr[1])*30)+($todayArr[2]-$dayArr[2]);
//距离的秒数
$secs=$now-$time;

if($todayArr[0]-$dayArr[0]>0 && $days>3){//跨年且超过3天
return date('Y-m-d',$time);
}else{

if($days<1){//今天
if($secs<60)return $secs.'秒前';
elseif($secs<3600)return floor($secs/60)."分钟前";
else return floor($secs/3600)."小时前";
}else if($days<2){//昨天
$hour=date('h',$time);
return "昨天".$hour.'点';
}elseif($days<3){//前天
$hour=date('h',$time);
return "前天".$hour.'点';
}else{//三天前
return date('m月d号',$time);
}
}
}
Copy after login

Code to convert PHP time to timestamp

Available in Chinese. You need to use regular expressions to replace Chinese characters. Because that's not a standard format.

can be processed with the following code.

$date = "15:12 on November 1, 2009"; //Must be a standard double-digit date
$date = ereg_replace("[^0-9]","", $date); //Filter non-numbers
$time = strtotime($date); //The variable $time is the timestamp

However. If you are using the current time. It's faster to just write like this.
$time = time();

The php date function can format the timestamp into 2012-5-3

date('Y-n-j', time()); // The time() function generates a timestamp of the current time, which can be replaced with your own timestamp

Reference: cn2.php .net/manual/zh/function.date.php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/898281.htmlTechArticlePHP formatted timestamp displays friendly time implementation ideas and codes. In PHP ideas, the time in the project is always displayed as 2014 -10-20 10:22 Looks very dull. On websites such as Weibo and QQ space, it usually appears...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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 to format c drive with dos command How to format c drive with dos command Feb 19, 2024 pm 04:23 PM

DOS command is a command line tool used in Windows operating system, which can be used to perform various system management tasks and operations. One of the common tasks is to format the hard drive, including the C drive. Formatting the C drive is a relatively dangerous operation because it will erase all data on the C drive and reinitialize the file system. Before performing this operation, make sure you have backed up important files and have a clear understanding of the impact that formatting will have on your computer. The following is formatted in the DOS command line

Golang time processing: How to convert timestamp to string in Golang Golang time processing: How to convert timestamp to string in Golang Feb 24, 2024 pm 10:42 PM

Golang time conversion: How to convert timestamp to string In Golang, time operation is one of the very common operations. Sometimes we need to convert the timestamp into a string for easy display or storage. This article will introduce how to use Golang to convert timestamps to strings and provide specific code examples. 1. Conversion of timestamps and strings In Golang, timestamps are usually expressed in the form of integer numbers, which represent the number of seconds from January 1, 1970 to the current time. The string is

Simple and effective steps to resolve 0x80070057 error Simple and effective steps to resolve 0x80070057 error Dec 27, 2023 am 08:38 AM

How to solve 0x80070057 error: simple and effective methods and steps Introduction: In the process of using the computer, we sometimes encounter various error codes. Among them, 0x80070057 is a very common error code, which is usually related to Windows operating system. This error code can appear in different situations, such as when installing or updating the operating system, backing up or restoring files, formatting drives, etc. Although this error code is frustrating, it's not unsolvable. This article will introduce

Revealed secrets of cell phone format recovery methods (mobile phone malfunction? Don't worry) Revealed secrets of cell phone format recovery methods (mobile phone malfunction? Don't worry) May 04, 2024 pm 06:01 PM

Nowadays, we will inevitably encounter some problems such as being unable to turn on the phone or lagging, such as system crash, but during use, mobile phones have become an indispensable part of our lives. We are often at a loss, and sometimes, there are no solutions to these problems. To help you solve cell phone problems, this article will introduce you to some methods of cell phone format recovery and restore your phone to normal operation. Back up data - protect important information, such as photos and contacts, from being lost during the formatting process. Before formatting your phone, the first thing to consider is to back up important data and files on your phone. To ensure data security, or choose to transfer files to a cloud storage service, you can back it up by connecting to a computer. Use the system's built-in recovery function - simple

Will formatting a laptop make it faster? Will formatting a laptop make it faster? Feb 12, 2024 pm 11:54 PM

Will formatting a laptop make it faster? If you want to format your Windows laptop but want to know if it will make it faster, this article will help you know the right answer to this question. Will formatting a laptop make it faster? There are many reasons why users format their Windows laptops. But the most common reason is slow performance or speed of your laptop. Formatting a laptop will completely delete all data stored on the C drive or the hard drive partition where Windows operating system is installed. Therefore, every user will think twice before taking this step, especially when it comes to the performance of the laptop. This article will help you understand whether formatting your laptop will speed it up. Formatting your laptop helps

Computer formatting tutorial Computer formatting tutorial Jan 08, 2024 am 08:21 AM

Many times when using a computer, you will encounter too much garbage, but many users still don’t know how to format the computer. It doesn’t matter. Here is a tutorial on computer formatting for you to take a look at. How to format a computer: 1. Right-click "This PC" on the desktop and click "Manage". 2. Click "Storage" in "Computer Management" to open "Disk Management". 3. Select the hard drive you want to clean, right-click and select "Format". 4. Check "Perform Quick Format" and click "OK" to start formatting.

What are the methods of html formatting? What are the methods of html formatting? Mar 08, 2024 am 09:53 AM

HTML formatting method: 1. Use online HTML formatting tools; 2. Use the HTML formatting shortcut keys that come with the code editor, such as Shift + Alt + F in Visual Studio Code; 3. Use plug-ins, such as Sublime Text HTML/CSS/JS Prettify plug-in; 4. Use command line tools, such as HTML Tidy; 5. Manual formatting according to coding standards and habits.

How to match timestamps using regular expressions in Go? How to match timestamps using regular expressions in Go? Jun 02, 2024 am 09:00 AM

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

See all articles