Convert timestamps saved in database to past time format
This function is generally used to display the publication time of articles on Weibo and blogs. For example: If a Weibo post is posted on Sina Weibo, if it was posted today, it will display XX seconds and XX hours ago instead of the specific time. If you repost a previously published Weibo, the original Weibo will display the time it was posted.
So how to achieve it? The code is as follows:
<code><span>/**格式化时间函数 *<span> @param</span> $time 需要格式化的时间戳 */</span><span><span>function</span><span>time_format</span><span>(<span>$time</span>)</span> {</span><span>$now</span> = time(); <span>$tody</span> = strtotime(date(<span>'Y-m-d'</span>)); <span>$diff</span> = <span>$now</span> - <span>$time</span>; <span>$str</span> = <span>''</span>; <span>switch</span> (<span>$time</span>) { <span>case</span><span>$diff</span> < <span>60</span>: <span>$str</span> = <span>$diff</span> . <span>'秒前'</span>; <span>break</span>; <span>case</span><span>$diff</span> < <span>3600</span>: <span>$str</span> = floor(<span>$diff</span> / <span>60</span>) . <span>'分钟前'</span>; <span>break</span>; <span>case</span><span>$diff</span> < (<span>3600</span> * <span>8</span>): <span>$str</span> = floor(<span>$diff</span> / <span>3600</span>) . <span>'小时前'</span>; <span>break</span>; <span>case</span><span>$time</span> > <span>$tody</span>: <span>$str</span> = <span>'今天'</span> . date(<span>'Y-m-d H:i:s'</span>, <span>$time</span>); <span>break</span>; <span>default</span>: <span>$str</span> = date(<span>'Y-m-d H:i:s'</span>, <span>$time</span>); } <span>return</span><span>$str</span>; } <span>$time</span> = <span>1423110837</span>;<span>//模拟保存在数据库中的时间戳</span><span>echo</span> time_format(<span>$time</span>);</code>
Interpretation:
1. First we need to get the current time:
3. Get the difference between the timestamp in the database and the current time. That is:
4. Use switch to determine the timestamp in the database to display the corresponding time format (hour, minute, second, specific time)
The above introduces the conversion of timestamps saved in the database into past time format, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Today we are mainly going to take a look at the time application method of golang time package. The general rule between the two is that "wall time" is used to tell time, and "monotonic clock" is used to measure time; there are other clock processing methods.

Under Linux, it is very difficult to directly use the svndiff command to view code modifications, so I searched for a better solution on the Internet, which is to use vimdiff as a code viewing tool for svndiff, especially for those who are accustomed to using vim. It is very convenient. When using the svndiff command to compare the modifications of a certain file, for example, if you execute the following command: $svndiff-r4420ngx_http_limit_req_module.c, the following command will actually be sent to the default diff program: -u-Lngx_http_limit_req_module.c(revision4420)-Lngx_

When the HMD Skyline(available on Amazon for $499) was launched last month, it was released in two colors - Neon Pink and Twisted Black. They are now joined by a third color dubbed Blue Topaz. HMD Global has also announced an official case for the ph

1The basic unit of Unicode computer storage is the byte, which is composed of 8 bits. Since English only consists of 26 letters plus a number of symbols, English characters can be stored directly in bytes. But other languages (such as Chinese, Japanese, Korean, etc.) have to use multiple bytes for encoding due to the large number of characters. With the spread of computer technology, non-Latin character encoding technology continues to develop, but there are still two major limitations: no multi-language support: the encoding scheme of one language cannot be used in another language and there is no unified standard: for example There are many encoding standards in Chinese such as GBK, GB2312, GB18030, etc. Since the encoding methods are not unified, developers need to convert back and forth between different encodings, and many errors will inevitably occur.

Switchcase requires specific code examples to determine variables. In programming, we often need to perform different operations based on different variable values. The switchcase statement is a convenient structure that allows you to select different blocks of code for execution based on the value of a variable. The following is a specific code example that shows how to use the switchcase statement to determine different values of variables: #includeintmain(){

1. Overview As part of this article, let us start with some problems with the existing Date and CalendarAPI and explore how the new Java8Date and TimeAPI solve these problems. We will also take a look at the core classes in the Java8 time class library, such as LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Period, Duration and their APIs. 2. The problem of thread safety of the old time API (before Java 8)-Date and Calendar classes are not thread-safe, making it difficult for developers to debug concurrency problems of these APIs and need to write additional code to deal with them.

1. Two ways to represent time in Python: timestamp: offset in seconds relative to 1970.1.100:00:00, unique time tuple struct_time: a total of 9 elements>tm_year: year 1-12> tm_mon: month 1-12>tm_mday: day 1-31>tm_hour: hour 0-23>tm_min: minute 0-59>tm_sec: second 0-59>tm_wday: week 0-6 (0 means Sunday)>tm_day: Day of the year 1-366>tm_isdst: whether it is daylight saving, the default is -1.ti

What are the similarities and differences between __str__ and __repr__? We all know the representation of strings. Python's built-in function repr() can express objects in the form of strings to facilitate our identification. This is the "string representation". repr() obtains the string representation of an object through the special method __repr__. If __repr__ is not implemented, when we print an instance of a vector to the console, the resulting string may be. >>>classExample:pass>>>print(str(Example()))>>>
