Heim >Backend-Entwicklung >PHP-Tutorial >Allgemeine PHP-Funktion – Zeitleiste für die Datumsgenerierung

Allgemeine PHP-Funktion – Zeitleiste für die Datumsgenerierung

WBOY
WBOYOriginal
2016-08-08 09:27:511468Durchsuche

<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> < 3600<span>) {
</span><span>22</span>                     <span>//</span><span>一分钟内</span>
<span>23</span>                     <span>if</span> (<span>$seconds</span> < 60<span>) {
</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> }

Das Obige stellt die allgemeine Funktion der PHP-Zeitleiste zur Datumsgenerierung vor, einschließlich des Inhalts. Ich hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn