search
HomeBackend DevelopmentPHP TutorialPHP general function - date generation timeline

<span> 1</span> <span>/*</span><span>*
</span><span> 2</span> <span> * 时间轴函数, Unix 时间戳
</span><span> 3</span> <span> * @param int $time 时间
</span><span> 4</span>  <span>*/</span>
<span> 5</span> <span>function</span> TranTime(<span>$time</span><span>) {
</span><span> 6</span>     <span>//</span><span>$time = strtotime($time);</span>
<span> 7</span>     <span>$nowTime</span> = <span>time</span><span> (); 
</span><span> 8</span>     <span>$message</span> = ''<span>; 
</span><span> 9</span>     <span>//</span><span>一年前</span>
<span>10</span>     <span>if</span> (<span>idate</span> ( 'Y', <span>$nowTime</span> ) != <span>idate</span> ( 'Y', <span>$time</span><span> )) {
</span><span>11</span>         <span>$message</span> = <span>date</span> ( 'Y年m月d日', <span>$time</span><span> );
</span><span>12</span> <span>    }
</span><span>13</span>     <span>else</span><span> {
</span><span>14</span>         <span>//</span><span>同一年</span>
<span>15</span>         <span>$days</span> = <span>idate</span> ( 'z', <span>$nowTime</span> ) - <span>idate</span> ( 'z', <span>$time</span><span> );
</span><span>16</span>         <span>switch</span>(<span>true</span><span>){
</span><span>17</span>             <span>//</span><span>一天内</span>
<span>18</span>             <span>case</span> (0 == <span>$days</span>):
<span>19</span>                 <span>$seconds</span> = <span>$nowTime</span> - <span>$time</span><span>;
</span><span>20</span>                 <span>//</span><span>一小时内</span>
<span>21</span>                 <span>if</span> (<span>$seconds</span> ) {
<span>22</span>                     <span>//</span><span>一分钟内</span>
<span>23</span>                     <span>if</span> (<span>$seconds</span> ) {
<span>24</span>                         <span>if</span> (3 > <span>$seconds</span><span>) {
</span><span>25</span>                             <span>$message</span> = '刚刚'<span>;
</span><span>26</span>                         } <span>else</span><span> {
</span><span>27</span>                             <span>$message</span> = <span>$seconds</span> . '秒前'<span>;
</span><span>28</span> <span>                        }
</span><span>29</span> <span>                    }
</span><span>30</span>                     <span>$message</span> = <span>intval</span> ( <span>$seconds</span> / 60 ) . '分钟前'<span>;
</span><span>31</span> <span>                }
</span><span>32</span>                 <span>$message</span> = <span>idate</span> ( 'H', <span>$nowTime</span> ) - <span>idate</span> ( 'H', <span>$time</span> ) . '小时前'<span>;
</span><span>33</span>                 <span>break</span><span>;
</span><span>34</span>                 <span>//</span><span>昨天</span>
<span>35</span>             <span>case</span> (1 == <span>$days</span>):
<span>36</span>                 <span>$message</span> = '昨天' . <span>date</span> ( 'H:i', <span>$time</span><span> );
</span><span>37</span>                 <span>break</span><span>;
</span><span>38</span>                 <span>//</span><span>前天</span>
<span>39</span>             <span>case</span> (2 == <span>$days</span>):
<span>40</span>                 <span>$message</span> = '前天 ' . <span>date</span> ( 'H:i', <span>$time</span><span> );
</span><span>41</span>                 <span>break</span><span>;
</span><span>42</span>                 <span>//</span><span>7天内</span>
<span>43</span>             <span>case</span> (7 > <span>$days</span>):
<span>44</span>                 <span>$message</span> = <span>$days</span> . '天前'<span>;
</span><span>45</span>                 <span>break</span><span>;
</span><span>46</span>                 <span>//</span><span>超过7天</span>
<span>47</span>             <span>default</span>:
<span>48</span>                 <span>$message</span> = <span>date</span> ( 'n月j日 H:i', <span>$time</span><span> );
</span><span>49</span>                 <span>break</span><span>;
</span><span>50</span> <span>        }
</span><span>51</span> <span>    }
</span><span>52</span>     <span>return</span> <span>$message</span><span>;
</span><span>53</span> }

The above introduces the general function of PHP - date generation timeline, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
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
time包的单调时钟处理time包的单调时钟处理Aug 04, 2023 pm 05:45 PM

我们今天主要是来看一看golang time 包的时间应用方式。两者的一般规则是「wall time」用于告知时间,而「monotonic clock」用于测量时间;除外还有其他的时钟处理方式。

vue3中怎么使用element-plus调用messagevue3中怎么使用element-plus调用messageMay 17, 2023 pm 03:52 PM

vue3使用element-plus调用message环境:vue3+typescript+element-plus1.全局引入element之后element已经在app.config.globalProperties添加了全局方法$message所以在optionsAPI中可以直接使用mounted(){(thisasany).$message.success("this.$message");}2.在CompositionAPI中setup方法传入了两个变量props和

Java8 Time Api如何使用Java8 Time Api如何使用Apr 28, 2023 pm 12:25 PM

1.概述作为本文的一部分,让我们从现有Date和CalendarAPI存在的一些问题入手,来探讨新的Java8Date和TimeAPI如何解决这些问题。我们还将搞一搞Java8时间类库中的核心类,比如LocalDate,LocalTime,LocalDateTime,ZonedDateTime,Period,Duration以及它们的api。2.旧的时间API(java8之前)的问题线程安全-Date和Calendar类不是线程安全的,使开发者难以调试这些api的并发问题,需要编写额外的代码来处

Python time模块时间怎么获取和转换Python time模块时间怎么获取和转换May 13, 2023 pm 12:19 PM

Pythontime模块时间获取和转换Python的Time库可以进行时间相关的处理,如访问当前日期和时间,输出不同格式的时间以及等待指定的时间等。1.获取时间1.1.时间戳importtimetimestamp=time.time()#1682737552.5009851格林威治时间(GMT)1970年01月01日00时00分00秒起至现在的总秒数1.2.结构化时间importtimestruct_time=time.localtime()#time.struct_time(tm_year=2

python中time和datetime的区别及用法是什么python中time和datetime的区别及用法是什么May 02, 2023 am 11:01 AM

一.Python中表示时间的两种方式:时间戳:相对于1970.1.100:00:00以秒计算的偏移量,唯一的时间元组struct_time:共有9个元素>tm_year:年1-12>tm_mon:月1-12>tm_mday:日1-31>tm_hour:时0-23>tm_min:分0-59>tm_sec:秒0-59>tm_wday:星期0-6(0表示周日)>tm_day:一年中的第几天1-366>tm_isdst:是否是夏令,默认为-1二.ti

如何解决 golang 中的 “undefined: time.After” 错误?如何解决 golang 中的 “undefined: time.After” 错误?Jun 25, 2023 pm 05:28 PM

Golang是一门非常受欢迎的编程语言,其简单易学、高效快速的特性吸引了越来越多的开发者。但在使用中,不可避免地会遇到一些问题和错误。例如,使用time包中的After方法时,可能会遇到undefined:time.After的错误。本篇文章将为大家介绍如何解决这个错误。了解错误原因在Golang中,如果我们使用了一个未导出的函数名或未正

使用PHP函数 "time" 返回当前的UNIX时间戳使用PHP函数 "time" 返回当前的UNIX时间戳Jul 25, 2023 pm 04:42 PM

使用PHP函数"time"返回当前的UNIX时间戳UNIX时间戳是指从协调世界时(UTC)1970年1月1日0时0分0秒起至今的总秒数。在PHP中,可以使用内置函数"time"来获取当前的UNIX时间戳。本文将介绍如何使用这个函数,并提供相应的代码示例。代码示例:&lt;?php$timestamp=time();echo&quot;当前的

Go语言基础之time包详解Go语言基础之time包详解Jul 21, 2023 pm 01:27 PM

time.Now()返回的是一个Time类型。有时候别人给我们的是时间戳,我们需要反着转以下,时间戳-时间类型。需要用到的是time.Unix。

See all articles

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 Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment