Maison > développement back-end > tutoriel php > PHP通用函数 - 日期生成时间轴

PHP通用函数 - 日期生成时间轴

WBOY
Libérer: 2016-08-08 09:27:51
original
1458 Les gens l'ont consulté

<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> }
Copier après la connexion

以上就介绍了PHP通用函数 - 日期生成时间轴,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal