Rumah >pembangunan bahagian belakang >tutorial php >PHP通用函数,php通用_PHP教程
<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> }