Maison >développement back-end >tutoriel php >PHP得到某个时间段时间戳和mktime()函数
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></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教程有兴趣的朋友有所帮助。