mktime() 函數傳回日期的 Unix 時間戳記。 Unix 時間戳包含 Unix 紀元(1970 年 1 月 1 日 00:00:00 GMT)與指定時間之間的秒數。
語法:
mktime(hour,minute,second,month,day,year);
實例 :
<code><span><span><?php</span><span>$d</span> = mktime(<span>9</span>, <span>12</span>, <span>31</span>, <span>6</span>, <span>10</span>, <span>2015</span>); <span>echo</span><span>"创建日期是 "</span> . date(<span>"Y-m-d H:i:s"</span>, <span>$d</span>); <span>?></span></span></code>
常用時間戳函數
<code><span>/** * 得到开始和结束unix时间段 */</span> public function get_start_and_end_time(<span>$mark</span>) { <span>if</span>(<span>$mark</span> == <span>'yesterday'</span>) { <span>$start_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>) - <span>1</span>,<span>date</span>(<span>'Y'</span>)); <span>$end_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>),<span>date</span>(<span>'Y'</span>)) - <span>1</span>; }<span>else</span><span>if</span>(<span>$mark</span> == <span>'today'</span>) { <span>$start_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>),<span>date</span>(<span>'Y'</span>)); <span>$end_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>) + <span>1</span>,<span>date</span>(<span>'Y'</span>)) - <span>1</span>; }<span>else</span><span>if</span>(<span>$mark</span> == <span>'toweek'</span>) { <span>$start_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>) - <span>date</span>(<span>'w'</span>) + <span>1</span>,<span>date</span>(<span>'Y'</span>)); <span>$end_time</span> = mktime(<span>23</span>,<span>59</span>,<span>59</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'d'</span>) - <span>date</span>(<span>'w'</span>) + <span>7</span>,<span>date</span>(<span>'Y'</span>)); }<span>else</span><span>if</span>(<span>$mark</span> == <span>'tomonth'</span>) { <span>$start_time</span> = mktime(<span>0</span>,<span>0</span>,<span>0</span>,<span>date</span>(<span>'m'</span>),<span>1</span>,<span>date</span>(<span>'Y'</span>)); <span>$end_time</span> = mktime(<span>23</span>,<span>59</span>,<span>59</span>,<span>date</span>(<span>'m'</span>),<span>date</span>(<span>'t'</span>),<span>date</span>(<span>'Y'</span>)); } <span>$arr</span>[<span>'start_time'</span>] = <span>$start_time</span>; <span>$arr</span>[<span>'end_time'</span>] = <span>$end_time</span>; <span>return</span><span>$arr</span>; }</code>
以上就介紹了PHP得到某個時間段時間戳記和mktime()函數,包含了方面的內容,希望對PHP教程有興趣的朋友有所幫助。