>  기사  >  php教程  >  현재 연도, 월, 일의 시작 타임스탬프와 다음 연도, 월, 일의 시작 타임스탬프를 가져오는 PHP 함수

현재 연도, 월, 일의 시작 타임스탬프와 다음 연도, 월, 일의 시작 타임스탬프를 가져오는 PHP 함수

WBOY
WBOY원래의
2016-09-09 08:13:351347검색

1. 올해의 타임스탬프

2. 이번 달의 타임스탬프

3. 오늘의 타임스탬프

4. 내년 시작 타임스탬프

5. 다음 달의 시작 타임스탬프

6. 내일 시작 타임스탬프

7. 현재 타임스탬프

기능 코드:

<span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 获取时间戳
 * $Ymd = Y 年
 * $Ymd = m 月
 * $Ymd = d 日
 * $Ymd = NULL 当前时间戳
 * $xia = true 是否取下次开始时间戳:取下年开始时间戳 或者下月开始时间戳  或者明日开始时间戳
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span> getTime(<span style="color: #800080;">$Ymd</span>=<span style="color: #0000ff;">NULL</span>,<span style="color: #800080;">$xia</span>=<span style="color: #0000ff;">false</span><span style="color: #000000;">){
    </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$Ymd</span>=='Y' && <span style="color: #800080;">$xia</span>==<span style="color: #0000ff;">true</span><span style="color: #000000;">){
        </span><span style="color: #008000;">//</span><span style="color: #008000;">取下个年度开始时间戳</span>
        <span style="color: #0000ff;">return</span> <span style="color: #008080;">strtotime</span>((<span style="color: #008080;">date</span>('Y',<span style="color: #008080;">time</span>())+1).'-01-01 00:00:00'<span style="color: #000000;">);
    }
    </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$Ymd</span>=='Y'<span style="color: #000000;">){
        </span><span style="color: #008000;">//</span><span style="color: #008000;">取本年度开始时间戳</span>
        <span style="color: #0000ff;">return</span> <span style="color: #008080;">strtotime</span>(<span style="color: #008080;">date</span>('Y',<span style="color: #008080;">time</span>()).'-01-01 00:00:00'<span style="color: #000000;">);
    }
    </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$Ymd</span>=='m' && <span style="color: #800080;">$xia</span>==<span style="color: #0000ff;">true</span><span style="color: #000000;">){
        </span><span style="color: #008000;">//</span><span style="color: #008000;">取下个月度开始时间戳</span>
        <span style="color: #800080;">$xiayue_nianfen</span>    =    <span style="color: #008080;">date</span>('Y',<span style="color: #008080;">time</span><span style="color: #000000;">());
        </span><span style="color: #800080;">$xiayue_yuefen</span>    =    <span style="color: #008080;">date</span>('m',<span style="color: #008080;">time</span><span style="color: #000000;">());
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$xiayue_yuefen</span>==12<span style="color: #000000;">){
            </span><span style="color: #800080;">$xiayue_nianfen</span>    +=    1;    <span style="color: #008000;">//</span><span style="color: #008000;">如果月份等于12月,那么下月年份+1</span>
            <span style="color: #800080;">$xiayue_yuefen</span>    =    1;    <span style="color: #008000;">//</span><span style="color: #008000;">如果月份等于12月,那么下月月份=1月</span>
<span style="color: #000000;">        }
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #800080;">$xiayue_yuefen</span>    +=    1;    <span style="color: #008000;">//</span><span style="color: #008000;">如果月份不是12月,那么在当前月份上+1</span>
<span style="color: #000000;">        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">strtotime</span>(<span style="color: #800080;">$xiayue_nianfen</span>.'-'.<span style="color: #800080;">$xiayue_yuefen</span>.'-01 00:00:00'<span style="color: #000000;">);
    }
    </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$Ymd</span>=='m'<span style="color: #000000;">){
        </span><span style="color: #008000;">//</span><span style="color: #008000;">取本月度开始时间戳</span>
        <span style="color: #0000ff;">return</span> <span style="color: #008080;">strtotime</span>(<span style="color: #008080;">date</span>('Y-m',<span style="color: #008080;">time</span>()).'-01 00:00:00'<span style="color: #000000;">);
    }
    </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$Ymd</span>=='d' && <span style="color: #800080;">$xia</span>==<span style="color: #0000ff;">true</span><span style="color: #000000;">){
        </span><span style="color: #008000;">//</span><span style="color: #008000;">取明日开始时间戳</span>
        <span style="color: #0000ff;">return</span> <span style="color: #008080;">strtotime</span>(<span style="color: #008080;">date</span>('Y-m-d',<span style="color: #008080;">time</span>()).' 00:00:00')+86400<span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$Ymd</span>=='d'<span style="color: #000000;">){
        </span><span style="color: #008000;">//</span><span style="color: #008000;">取今日开始时间戳</span>
        <span style="color: #0000ff;">return</span> <span style="color: #008080;">strtotime</span>(<span style="color: #008080;">date</span>('Y-m-d',<span style="color: #008080;">time</span>()).' 00:00:00'<span style="color: #000000;">);
    }
    </span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
        </span><span style="color: #008000;">//</span><span style="color: #008000;">取当前时间戳</span>
        <span style="color: #0000ff;">return</span> <span style="color: #008080;">time</span><span style="color: #000000;">();
    }
}</span>

통화 코드:

getTime('Y');        <span style="color: #008000;">//</span><span style="color: #008000;">当前年的时间戳</span>
getTime('m');        <span style="color: #008000;">//</span><span style="color: #008000;">当前月的时间戳</span>
getTime('d');        <span style="color: #008000;">//</span><span style="color: #008000;">当前日的时间戳</span>
getTime('Y',<span style="color: #0000ff;">true</span>);    <span style="color: #008000;">//</span><span style="color: #008000;">明年的时间戳</span>
getTime('m',<span style="color: #0000ff;">true</span>);    <span style="color: #008000;">//</span><span style="color: #008000;">下月的时间戳</span>
getTime('d',<span style="color: #0000ff;">true</span>);    <span style="color: #008000;">//</span><span style="color: #008000;">明日的时间戳</span>
getTime();            <span style="color: #008000;">//</span><span style="color: #008000;">当前的时间戳</span>

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