>백엔드 개발 >PHP 튜토리얼 >매우 실용적인 PHP 기능 요약 및 배열

매우 실용적인 PHP 기능 요약 및 배열

WBOY
WBOY원래의
2016-08-08 09:33:02872검색

1. PHP 암호화 및 복호화

PHP 암호화 및 복호화 기능을 사용하면 데이터베이스에 저장된 일부 유용한 문자열을 암호화하고 해당 문자열을 역으로 복호화할 수 있습니다. 이 기능은 base64 및 MD5 암호화 및 복호화를 사용합니다.

<span> 1</span> <span>function</span> encryptDecrypt(<span>$key</span>, <span>$string</span>, <span>$decrypt</span><span>){ 
</span><span> 2</span> 
<span> 3</span>     <span>if</span>(<span>$decrypt</span><span>){ 
</span><span> 4</span> 
<span> 5</span>         <span>$decrypted</span> = <span>rtrim</span>(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, <span>md5</span>(<span>$key</span>), <span>base64_decode</span>
<span> 6</span>        (<span>$string</span>), MCRYPT_MODE_CBC, <span>md5</span>(<span>md5</span>(<span>$key</span>))), "12"<span>); 
</span><span> 7</span> 
<span> 8</span>         <span>return</span> <span>$decrypted</span><span>; 
</span><span> 9</span> 
<span>10</span>     }<span>else</span><span>{ 
</span><span>11</span> 
<span>12</span>         <span>$encrypted</span> = <span>base64_encode</span>(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, <span>md5</span>(<span>$key</span>),
<span>13</span>        <span>$string</span>, MCRYPT_MODE_CBC, <span>md5</span>(<span>md5</span>(<span>$key</span><span>)))); 
</span><span>14</span> 
<span>15</span>         <span>return</span> <span>$encrypted</span><span>; 
</span><span>16</span> 
<span>17</span> <span>    } 
</span><span>18</span> 
<span>19</span> }

사용방법:

<span>1</span> <span>//</span><span>以下是将字符串“Helloweba欢迎您”分别加密和解密 
</span><span>2</span> 
<span>3</span> <span>//加密: </span>
<span>4</span> 
<span>5</span> <span>echo</span> encryptDecrypt('password', 'Helloweba欢迎您',0<span>); 
</span><span>6</span> 
<span>7</span> <span>//</span><span>解密: </span>
<span>8</span> 
<span>9</span> <span>echo</span> encryptDecrypt('password', 'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);

2. PHP는 임의의 문자열을 생성합니다

임의의 이름, 임시 비밀번호 및 기타 문자열을 생성해야 하는 경우 다음 기능을 사용할 수 있습니다.

<span> 1</span> <span>function</span> generateRandomString(<span>$length</span> = 10<span>) { 
</span><span> 2</span> 
<span> 3</span>     <span>$characters</span> = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'<span>; 
</span><span> 4</span> 
<span> 5</span>     <span>$randomString</span> = ''<span>; 
</span><span> 6</span> 
<span> 7</span>     <span>for</span> (<span>$i</span> = 0; <span>$i</span> < <span>$length</span>; <span>$i</span>++<span>) { 
</span><span> 8</span> 
<span> 9</span>         <span>$randomString</span> .= <span>$characters</span>[<span>rand</span>(0, <span>strlen</span>(<span>$characters</span>) - 1<span>)]; 
</span><span>10</span> 
<span>11</span> <span>    } 
</span><span>12</span> 
<span>13</span>     <span>return</span> <span>$randomString</span><span>; 
</span><span>14</span> 
<span>15</span> }

사용방법:

<span>1</span> <span>echo</span> generateRandomString(20);

3. PHP는 파일 확장자(접미사)를 가져옵니다.

다음 함수를 사용하면 파일 확장자나 접미사를 빠르게 얻을 수 있습니다.

<span>1</span> <span>function</span> getExtension(<span>$filename</span><span>){ 
</span><span>2</span> 
<span>3</span>   <span>$myext</span> = <span>substr</span>(<span>$filename</span>, <span>strrpos</span>(<span>$filename</span>, '.'<span>)); 
</span><span>4</span> 
<span>5</span>   <span>return</span> <span>str_replace</span>('.','',<span>$myext</span><span>); 
</span><span>6</span> 
<span>7</span> }

사용방법:

<span>1</span> <span>$filename</span> = '我的文档.doc'<span>; 
</span><span>2</span> 
<span>3</span> <span>echo</span> getExtension(<span>$filename</span>);

4. PHP가 파일 크기를 가져와서 형식을 지정합니다.

아래에 사용된 함수를 사용하면 파일 크기를 가져와 읽기 쉬운 KB, MB 및 기타 형식으로 변환할 수 있습니다.

<span> 1</span> <span>function</span> formatSize(<span>$size</span><span>) { 
</span><span> 2</span> 
<span> 3</span>     <span>$sizes</span> = <span>array</span>(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"<span>); 
</span><span> 4</span> 
<span> 5</span>     <span>if</span> (<span>$size</span> == 0<span>) {  
</span><span> 6</span> 
<span> 7</span>         <span>return</span>('n/a'<span>);  
</span><span> 8</span> 
<span> 9</span>     } <span>else</span><span> { 
</span><span>10</span> 
<span>11</span>       <span>return</span> (<span>round</span>(<span>$size</span>/<span>pow</span>(1024, (<span>$i</span> = <span>floor</span>(<span>log</span>(<span>$size</span>, 1024)))), 2) . <span>$sizes</span>[<span>$i</span><span>]);  
</span><span>12</span> 
<span>13</span> <span>    } 
</span><span>14</span> 
<span>15</span> }

사용방법:

<span>1</span> <span>$thefile</span> = <span>filesize</span>('test_file.mp3'<span>); 
</span><span>2</span> 
<span>3</span> <span>echo</span> formatSize(<span>$thefile</span>);

5. PHP 태그 문자 교체

때때로 문자열과 템플릿 태그를 지정된 콘텐츠로 바꿔야 하는 경우 다음 기능을 사용할 수 있습니다.

<span> 1</span> <span>function</span> stringParser(<span>$string</span>,<span>$replacer</span><span>){ 
</span><span> 2</span> 
<span> 3</span>     <span>$result</span> = <span>str_replace</span>(<span>array_keys</span>(<span>$replacer</span>), <span>array_values</span>(<span>$replacer</span>),<span>$string</span><span>); 
</span><span> 4</span> 
<span> 5</span>     <span>return</span> <span>$result</span><span>; 
</span><span> 6</span> 
<span> 7</span> <span>}
</span><span> 8</span> 
<span> 9</span> <span>//</span><span>使用方法如下</span>
<span>10</span> 
<span>11</span> <span>$string</span> = '<span>The {b}anchor text{/b} is the {b}actual word{/b} or words used 
</span><span>12</span> {br}to describe the link {br}itself'<span>; 
</span><span>13</span> 
<span>14</span> <span>$replace_array</span> = <span>array</span>('{b}' => '<b>','{/b}' => '</b>','{br}' => '<br >'<span>); 
</span><span>15</span> 
<span>16</span>  
<span>17</span> 
<span>18</span> <span>echo</span> stringParser(<span>$string</span>,<span>$replace_array</span>);</br >

6. PHP는 디렉터리에 파일 이름을 나열합니다.

디렉터리의 모든 파일을 나열하려면 다음 코드를 사용하세요.

<span> 1</span> <span>function</span> listDirFiles(<span>$DirPath</span><span>){ 
</span><span> 2</span> 
<span> 3</span>     <span>if</span>(<span>$dir</span> = <span>opendir</span>(<span>$DirPath</span><span>)){ 
</span><span> 4</span> 
<span> 5</span>          <span>while</span>((<span>$file</span> = <span>readdir</span>(<span>$dir</span>))!== <span>false</span><span>){ 
</span><span> 6</span> 
<span> 7</span>                 <span>if</span>(!<span>is_dir</span>(<span>$DirPath</span>.<span>$file</span><span>)) 
</span><span> 8</span> 
<span> 9</span> <span>                { 
</span><span>10</span> 
<span>11</span>                     <span>echo</span> "filename: <span>$file</span><br >"<span>; 
</span><span>12</span> 
<span>13</span> <span>                } 
</span><span>14</span> 
<span>15</span> <span>         } 
</span><span>16</span> 
<span>17</span> <span>    } 
</span><span>18</span> 
<span>19</span> <span>}
</span><span>20</span> 
<span>21</span> <span>//</span><span>使用方法如下</span>
<span>22</span> 
<span>23</span> listDirFiles('home/some_folder/');

7. PHP가 현재 페이지 URL을 가져옵니다

다음 함수는 http인지 https인지 현재 페이지의 URL을 가져올 수 있습니다.

<span> 1</span> <span>function</span><span> curPageURL() { 
</span><span> 2</span> 
<span> 3</span>     <span>$pageURL</span> = 'http'<span>; 
</span><span> 4</span> 
<span> 5</span>     <span>if</span> (!<span>empty</span>(<span>$_SERVER</span>['HTTPS'])) {<span>$pageURL</span> .= "s"<span>;} 
</span><span> 6</span> 
<span> 7</span>     <span>$pageURL</span> .= "://"<span>; 
</span><span> 8</span> 
<span> 9</span>     <span>if</span> (<span>$_SERVER</span>["SERVER_PORT"] != "80"<span>) { 
</span><span>10</span> 
<span>11</span>         <span>$pageURL</span> .= <span>$_SERVER</span>["SERVER_NAME"].":".<span>$_SERVER</span>["SERVER_PORT"].<span>$_SERVER</span>
<span>12</span> ["REQUEST_URI"<span>]; 
</span><span>13</span> 
<span>14</span>     } <span>else</span><span> { 
</span><span>15</span> 
<span>16</span>         <span>$pageURL</span> .= <span>$_SERVER</span>["SERVER_NAME"].<span>$_SERVER</span>["REQUEST_URI"<span>]; 
</span><span>17</span> 
<span>18</span> <span>    } 
</span><span>19</span> 
<span>20</span>     <span>return</span> <span>$pageURL</span><span>; 
</span><span>21</span> 
<span>22</span> <span>}
</span><span>23</span> 
<span>24</span> <span>//</span><span>使用方法如下</span>
<span>25</span> 
<span>26</span> <span>echo</span> curPageURL();

8. PHP는 파일을 강제로 다운로드합니다

때때로 브라우저가 PDF 파일과 같은 파일을 직접 열지 않고 파일을 직접 다운로드하기를 원하는 경우 다음 기능을 사용하여 파일을 강제로 다운로드할 수 있습니다. application/octet-stream 헤더 유형. 함수에 사용됩니다.

<span> 1</span> <span>function</span> download(<span>$filename</span><span>){ 
</span><span> 2</span> 
<span> 3</span>     <span>if</span> ((<span>isset</span>(<span>$filename</span>))&&(<span>file_exists</span>(<span>$filename</span><span>))){ 
</span><span> 4</span> 
<span> 5</span>        <span>header</span>("Content-length: ".<span>filesize</span>(<span>$filename</span><span>)); 
</span><span> 6</span> 
<span> 7</span>        <span>header</span>('Content-Type: application/octet-stream'<span>); 
</span><span> 8</span> 
<span> 9</span>        <span>header</span>('Content-Disposition: attachment; filename="' . <span>$filename</span> . '"'<span>); 
</span><span>10</span> 
<span>11</span>        <span>readfile</span>("<span>$filename</span>"<span>); 
</span><span>12</span> 
<span>13</span>     } <span>else</span><span> { 
</span><span>14</span> 
<span>15</span>        <span>echo</span> "Looks like file does not exist!"<span>; 
</span><span>16</span> 
<span>17</span> <span>    } 
</span><span>18</span> 
<span>19</span> <span>}
</span><span>20</span> 
<span>21</span> <span>//</span><span>使用方法如下</span>
<span>22</span> 
<span>23</span> download('/down/test_45f73e852.zip'); 

9. PHP가 문자열 길이를 가로챕니다

문자열(한자 포함)의 길이를 가로채야 하는 상황이 종종 발생합니다. 예를 들어, 제목은 몇 글자 이상을 표시할 수 없습니다.... 다음 함수는 다음과 같습니다. 귀하의 요구를 충족하십시오.

<span> 1</span> <span>/*</span> 
<span> 2</span> 
<span> 3</span> <span> Utf-8、gb2312都支持的汉字截取函数 
</span><span> 4</span> 
<span> 5</span> <span> cut_str(字符串, 截取长度, 开始长度, 编码); 
</span><span> 6</span> 
<span> 7</span> <span> 编码默认为 utf-8 
</span><span> 8</span> 
<span> 9</span> <span> 开始长度默认为 0 
</span><span>10</span> 
<span>11</span> <span>*/</span> 
<span>12</span> 
<span>13</span> <span>function</span> cutStr(<span>$string</span>, <span>$sublen</span>, <span>$start</span> = 0, <span>$code</span> = 'UTF-8'<span>){ 
</span><span>14</span> 
<span>15</span>     <span>if</span>(<span>$code</span> == 'UTF-8'<span>){ 
</span><span>16</span> 
<span>17</span>         <span>$pa</span> = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"<span>; 
</span><span>18</span> 
<span>19</span>         <span>preg_match_all</span>(<span>$pa</span>, <span>$string</span>, <span>$t_string</span><span>); 
</span><span>20</span> 
<span>21</span>  
<span>22</span> 
<span>23</span>         <span>if</span>(<span>count</span>(<span>$t_string</span>[0]) - <span>$start</span> > <span>$sublen</span>) <span>return</span> <span>join</span>('', <span>array_slice</span>
<span>24</span> (<span>$t_string</span>[0], <span>$start</span>, <span>$sublen</span>))."..."<span>; 
</span><span>25</span> 
<span>26</span>         <span>return</span> <span>join</span>('', <span>array_slice</span>(<span>$t_string</span>[0], <span>$start</span>, <span>$sublen</span><span>)); 
</span><span>27</span> 
<span>28</span>     }<span>else</span><span>{ 
</span><span>29</span> 
<span>30</span>         <span>$start</span> = <span>$start</span>*2<span>; 
</span><span>31</span> 
<span>32</span>         <span>$sublen</span> = <span>$sublen</span>*2<span>; 
</span><span>33</span> 
<span>34</span>         <span>$strlen</span> = <span>strlen</span>(<span>$string</span><span>); 
</span><span>35</span> 
<span>36</span>         <span>$tmpstr</span> = ''<span>; 
</span><span>37</span> 
<span>38</span>  
<span>39</span> 
<span>40</span>         <span>for</span>(<span>$i</span>=0; <span>$i</span><<span>$strlen</span>; <span>$i</span>++<span>){ 
</span><span>41</span> 
<span>42</span>             <span>if</span>(<span>$i</span>>=<span>$start</span> && <span>$i</span><(<span>$start</span>+<span>$sublen</span><span>)){ 
</span><span>43</span> 
<span>44</span>                 <span>if</span>(<span>ord</span>(<span>substr</span>(<span>$string</span>, <span>$i</span>, 1))>129<span>){ 
</span><span>45</span> 
<span>46</span>                     <span>$tmpstr</span>.= <span>substr</span>(<span>$string</span>, <span>$i</span>, 2<span>); 
</span><span>47</span> 
<span>48</span>                 }<span>else</span><span>{ 
</span><span>49</span> 
<span>50</span>                     <span>$tmpstr</span>.= <span>substr</span>(<span>$string</span>, <span>$i</span>, 1<span>); 
</span><span>51</span> 
<span>52</span> <span>                } 
</span><span>53</span> 
<span>54</span> <span>            } 
</span><span>55</span> 
<span>56</span>             <span>if</span>(<span>ord</span>(<span>substr</span>(<span>$string</span>, <span>$i</span>, 1))>129) <span>$i</span>++<span>; 
</span><span>57</span> 
<span>58</span> <span>        } 
</span><span>59</span> 
<span>60</span>         <span>if</span>(<span>strlen</span>(<span>$tmpstr</span>)<<span>$strlen</span> ) <span>$tmpstr</span>.= "..."<span>; 
</span><span>61</span> 
<span>62</span>         <span>return</span> <span>$tmpstr</span><span>; 
</span><span>63</span> 
<span>64</span> <span>    } 
</span><span>65</span> 
<span>66</span> <span>}
</span><span>67</span> 
<span>68</span> <span>//</span><span>使用方法如下</span>
<span>69</span> 
<span>70</span> <span>$str</span> = "jQuery插件实现的加载图片和页面效果"<span>; 
</span><span>71</span> 
<span>72</span> <span>echo</span> cutStr(<span>$str</span>,16);

10. PHP가 클라이언트의 실제 IP를 가져옵니다

우리는 종종 사용자의 IP를 기록하기 위해 데이터베이스를 사용합니다. 다음 코드는 클라이언트의 실제 IP를 얻을 수 있습니다.

<span> 1</span> <span>//</span><span>获取用户真实IP </span>
<span> 2</span> 
<span> 3</span> <span>function</span><span> getIp() { 
</span><span> 4</span> 
<span> 5</span>     <span>if</span> (<span>getenv</span>("HTTP_CLIENT_IP") && <span>strcasecmp</span>(<span>getenv</span>("HTTP_CLIENT_IP"),
<span> 6</span>  "unknown"<span>)) 
</span><span> 7</span> 
<span> 8</span>         <span>$ip</span> = <span>getenv</span>("HTTP_CLIENT_IP"<span>); 
</span><span> 9</span> 
<span>10</span>     <span>else</span> 
<span>11</span> 
<span>12</span>         <span>if</span> (<span>getenv</span>("HTTP_X_FORWARDED_FOR") && <span>strcasecmp</span>(<span>getenv</span>("HTTP_X_FORWARDED_FOR"), "unknown"<span>)) 
</span><span>13</span> 
<span>14</span>             <span>$ip</span> = <span>getenv</span>("HTTP_X_FORWARDED_FOR"<span>); 
</span><span>15</span> 
<span>16</span>         <span>else</span> 
<span>17</span> 
<span>18</span>             <span>if</span> (<span>getenv</span>("REMOTE_ADDR") && <span>strcasecmp</span>(<span>getenv</span>("REMOTE_ADDR"), "unknown"<span>)) 
</span><span>19</span> 
<span>20</span>                 <span>$ip</span> = <span>getenv</span>("REMOTE_ADDR"<span>); 
</span><span>21</span> 
<span>22</span>             <span>else</span> 
<span>23</span> 
<span>24</span>                 <span>if</span> (<span>isset</span> (<span>$_SERVER</span>['REMOTE_ADDR']) && <span>$_SERVER</span>['REMOTE_ADDR'] && 
<span>25</span> <span>strcasecmp</span>(<span>$_SERVER</span>['REMOTE_ADDR'], "unknown"<span>)) 
</span><span>26</span> 
<span>27</span>                     <span>$ip</span> = <span>$_SERVER</span>['REMOTE_ADDR'<span>]; 
</span><span>28</span> 
<span>29</span>                 <span>else</span> 
<span>30</span> 
<span>31</span>                     <span>$ip</span> = "unknown"<span>; 
</span><span>32</span> 
<span>33</span>     <span>return</span> (<span>$ip</span><span>); 
</span><span>34</span> 
<span>35</span> <span>}
</span><span>36</span> 
<span>37</span> <span>//</span><span>使用方法如下</span>
<span>38</span> 
<span>39</span> <span>echo</span> getIp();

11. PHP는 SQL 삽입을 방지합니다

데이터베이스를 쿼리할 때 보안상의 이유로 악의적인 SQL 삽입을 방지하기 위해 일부 불법 문자를 필터링해야 합니다.

<span> 1</span> <span>function</span> injCheck(<span>$sql_str</span><span>) {  
</span><span> 2</span> 
<span> 3</span>     <span>$check</span> = <span>preg_match</span>('<span>/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into
</span><span> 4</span> |load_file|outfile/', <span>$sql_str</span><span>); 
</span><span> 5</span> 
<span> 6</span>     <span>if</span> (<span>$check</span><span>) { 
</span><span> 7</span> 
<span> 8</span>         <span>echo</span> '非法字符!!'<span>; 
</span><span> 9</span> 
<span>10</span>         <span>exit</span><span>; 
</span><span>11</span> 
<span>12</span>     } <span>else</span><span> { 
</span><span>13</span> 
<span>14</span>         <span>return</span> <span>$sql_str</span><span>; 
</span><span>15</span> 
<span>16</span> <span>    } 
</span><span>17</span> 
<span>18</span> <span>}
</span><span>19</span> 
<span>20</span> <span>//</span><span>使用方法如下</span>
<span>21</span> 
<span>22</span> <span>echo</span> injCheck('1 or 1=1');

12. PHP 페이지 프롬프트 및 점프

양식 작업을 수행할 때 편의를 위해 사용자에게 작업 결과를 묻는 메시지를 표시하고 관련 페이지로 이동해야 하는 경우가 있습니다.

<span> 1</span> <span>function</span> message(<span>$msgTitle</span>,<span>$message</span>,<span>$jumpUrl</span><span>){ 
</span><span> 2</span>     <span>$str</span> = '<!DOCTYPE HTML>'<span>; 
</span><span> 3</span>     <span>$str</span> .= '<html>'<span>; 
</span><span> 4</span>     <span>$str</span> .= '<head>'<span>; 
</span><span> 5</span>     <span>$str</span> .= '<meta charset="utf-8">'<span>; 
</span><span> 6</span>     <span>$str</span> .= '<title>页面提示</title>'<span>; 
</span><span> 7</span>     <span>$str</span> .= '<style type="text/css">'<span>; 
</span><span> 8</span>     <span>$str</span> .= '<span>*{margin:0; padding:0}a{color:#369; text-decoration:none;}a:hover{text-decoration:underline}body{height:100%; font:12px/18px Tahoma, Arial,  sans-serif; color:#424242; background:#fff}.message{width:450px; height:120px; 
</span><span> 9</span> <span>margin:16% auto; border:1px solid #99b1c4; background:#ecf7fb}.message h3{height:28px; 
</span><span>10</span> line-height:28px; background:#2c91c6; text-align:center; color:#fff; font-size:14px}.msg_txt{padding:10px; margin-top:8px}.msg_txt h4{line-height:26px; font-size:14px}.msg_txt h4.red{color:#f30}.msg_txt p{line-height:22px}'<span>; 
</span><span>11</span>     <span>$str</span> .= '</style>'<span>; 
</span><span>12</span>     <span>$str</span> .= '</head>'<span>; 
</span><span>13</span>     <span>$str</span> .= '<body>'<span>; 
</span><span>14</span>     <span>$str</span> .= '<div>'<span>; 
</span><span>15</span>     <span>$str</span> .= '<h3>'.<span>$msgTitle</span>.'</h3>'<span>; 
</span><span>16</span>     <span>$str</span> .= '<div>'<span>; 
</span><span>17</span>     <span>$str</span> .= '<h4>'.<span>$message</span>.'</h4>'<span>; 
</span><span>18</span>     <span>$str</span> .= '<p>系统将在 <span>3</span> 秒后自动跳转,如果不想等待,直接点击 <a href="{$jumpUrl}">这里</a> 跳转</p>'<span>; 
</span><span>19</span>     <span>$str</span> .= "<script>setTimeout('location.replace(\'".<span>$jumpUrl</span>."\')',2000)</script>"<span>; 
</span><span>20</span>     <span>$str</span> .= '</div>'<span>; 
</span><span>21</span>     <span>$str</span> .= '</div>'<span>; 
</span><span>22</span>     <span>$str</span> .= '</body>'<span>; 
</span><span>23</span>     <span>$str</span> .= '</html>'<span>; 
</span><span>24</span>     <span>echo</span> <span>$str</span><span>; 
</span><span>25</span> <span>}
</span><span>26</span> 
<span>27</span> <span>//</span><span>使用方法如下 </span>
<span>28</span> 
<span>29</span> message('操作提示','操作成功!','http://www.helloweba.com/');

13. PHP 계산 시간

시간을 처리할 때 현재 시간부터 특정 시점까지의 시간을 계산해야 합니다. 예를 들어 클라이언트의 실행 시간을 계산할 때 일반적으로 hh:mm:ss로 표현됩니다.

<span> 1</span> <span>function</span> changeTimeType(<span>$seconds</span><span>) { 
</span><span> 2</span> 
<span> 3</span>     <span>if</span> (<span>$seconds</span> > 3600<span>) { 
</span><span> 4</span> 
<span> 5</span>         <span>$hours</span> = <span>intval</span>(<span>$seconds</span> / 3600<span>); 
</span><span> 6</span> 
<span> 7</span>         <span>$minutes</span> = <span>$seconds</span> % 3600<span>; 
</span><span> 8</span> 
<span> 9</span>         <span>$time</span> = <span>$hours</span> . ":" . <span>gmstrftime</span>('%M:%S', <span>$minutes</span><span>); 
</span><span>10</span> 
<span>11</span>     } <span>else</span><span> { 
</span><span>12</span> 
<span>13</span>         <span>$time</span> = <span>gmstrftime</span>('%H:%M:%S', <span>$seconds</span><span>); 
</span><span>14</span> 
<span>15</span> <span>    } 
</span><span>16</span> 
<span>17</span>     <span>return</span> <span>$time</span><span>; 
</span><span>18</span> 
<span>19</span> <span>}
</span><span>20</span> 
<span>21</span> <span>//</span><span>使用方法如下</span>
<span>22</span> 
<span>23</span> <span>$seconds</span> = 3712<span>; 
</span><span>24</span> 
<span>25</span> <span>echo</span> changeTimeType(<span>$seconds</span>);

이상에서는 관련 내용을 포함하여 매우 실용적인 PHP 기능을 요약하여 소개했습니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.