首頁 > 後端開發 > php教程 > PHP得到某個時間段時間戳記和mktime()函數

PHP得到某個時間段時間戳記和mktime()函數

WBOY
發布: 2016-08-08 09:28:11
原創
1111 人瀏覽過

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教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板