PHP 일반 함수 - 날짜 생성 타임라인

WBOY
풀어 주다: 2016-08-08 09:27:51
원래의
1445명이 탐색했습니다.

<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> }
로그인 후 복사

위 내용은 PHP의 일반적인 기능인 날짜 생성 타임라인을 소개하며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿