ホームページ >バックエンド開発 >PHPチュートリアル >PHP は特定の時刻のタイムスタンプと mktime() 関数を取得します
mktime() 関数は、日付の Unix タイムスタンプを返します。 Unix タイムスタンプには、Unix エポック (1970 年 1 月 1 日 00:00:00 GMT) と指定された時刻の間の秒数が含まれます。
文法:
mktime(時,分,秒,月,日,年);
例:
<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 チュートリアルに興味のある友人に役立つことを願っています。