Home >类库下载 >PHP类库 >PHP function to get the starting timestamp of the current year, month, and day and the starting timestamp of the next year, month, and day

PHP function to get the starting timestamp of the current year, month, and day and the starting timestamp of the next year, month, and day

高洛峰
高洛峰Original
2016-10-10 10:52:021546browse

PHP function to get the start timestamp of the current year, month, and day and the start timestamp of the next year, month, and day

1. The timestamp of the current year

2. The timestamp of the current month

3. The timestamp of the current day

4. Next year’s start timestamp

5. Next month’s start timestamp

6. Tomorrow’s start timestamp

7. Current timestamp

Function code:

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

Calling code:

getTime('Y');        //当前年的时间戳
getTime('m');        //当前月的时间戳
getTime('d');        //当前日的时间戳
getTime('Y',true);    //明年的时间戳
getTime('m',true);    //下月的时间戳
getTime('d',true);    //明日的时间戳
getTime();            //当前的时间戳


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more